How do I get the name of the Font Family given the font file?

谁都会走 提交于 2019-12-10 19:27:30

问题


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:


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;


来源:https://stackoverflow.com/questions/15279285/how-do-i-get-the-name-of-the-font-family-given-the-font-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!