How do I get the name of the Font Family given the font file?
.net, c#, file, fonts, metadata
Solution
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;
Problem
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!