Font family name from font file

I have a .ttf file, I want to get the name of the font family.

+4
source share
2 answers

This is easiest to do by importing the System.Windows.Media namespace, which gives you more options for working with a simpler API than getting a font from ByteArray

using System.Windows.Media; String fontFilePath = "PATH TO YOUR FONT"; GlyphTypeface glyphTypeface = new GlyphTypeface(fontFileURI); String fontFamily = glyphTypeface.Win32FamilyNames[new System.Globalization.CultureInfo("en-us")]; String fontFace = glyphTypeface.Win32FaceNames[new System.Globalization.CultureInfo("en-us")]; Console.WriteLine("Font: " + fontFamily + " " + fontFace); 
+2
source

Here is what I used in the past, it was for a web application, so probably not quite what you want. The Font ttf file was stored in the database. You will need to replace [FONTASBYTEARRAY] with the actual byte [].

There is probably a much better way to get the ttf file into a font object, but this should help get you started.

 using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Text; using System.Runtime.InteropServices; namespace Utility { public class Font { public string GetFont(byte[] [FONTASBYTEARRAY]) { PrivateFontCollection fc = new PrivateFontCollection(); IntPtr pointer = Marshal.UnsafeAddrOfPinnedArrayElement([FONTASBYTEARRAY], 0); fc.AddMemoryFont(pointer, Convert.ToInt32([FONTASBYTEARRAY].Length)); System.Drawing.Font f = new System.Drawing.Font(fc.Families[0], 10); FontFamily ff = f.FontFamily; return ff.Name; } } } 
+1
source

Source: https://habr.com/ru/post/1304841/


All Articles