How do I get the name of the Font Family given the

2019-07-13 15:56发布

I have a set of font files with unpredictable filenames, so I can't deduce the real "Font Family" name from the file name. I need to therefore read the font metadata to extract the real "Font Family" name, in order to render this font file. I'm in C#.NET 4.0 WinForms.

I've seen the function GetFontInformation but I can't seem to find the P/Invoke headers for the same. All I have is the C++ version which is honestly hard to figure out. Any ideas?

The reason I cannot use the PrivateFontCollection class to parse through a font file for me, is that these are OTF fonts and .NET/GDI+ only supports TTF fonts!

1条回答
仙女界的扛把子
2楼-- · 2019-07-13 16:55

You need to add font to the (PrivateFontCollection) and then request for the FontFamily and get its Name property.

private static string GetFontNameFromFile(string filename)
{
    PrivateFontCollection fontCollection = new PrivateFontCollection();
    fontCollection.AddFontFile("path_to_font");
    return fontCollection.Families[0].Name;
}

Needed namespace :

using System.Drawing;
using System.Drawing.Text;
查看更多
登录 后发表回答