Get font name from FontFamily in WPF

rossisdead picture rossisdead · Oct 27, 2009 · Viewed 7.7k times · Source

I'm currently working on a little font organization/preview application for myself, however, I'm having a hard time getting the exact information I need.

I've found that I can load an external font by just creating a new FontFamily object with the font's file location as its source. However, I can't find a way to get a font's specific font name back. I know I can use FontFamily.FamilyNames to get the font's family name back, but that's useless to me when I have multiple fonts with the same family being displayed. I'd like to actually display the specific name for the specific font.

Is there any way to do this? I currently display the file name instead, but it's incredibly sloppy because I have to iterate through every file in a directory and call Fonts.GetFontFamilies() on each just so I can get the actual file name(FontFamily's Source property only gives WPF's makeshift family-name source instead of something useful).

Answer

Satya picture Satya · Jan 18, 2010

This is what I am doing:

        ListBoxItem listBoxItem = null;
        foreach (FontFamily fontFamily in Fonts.SystemFontFamilies)
        {
            listBoxItem = new ListBoxItem();
            listBoxItem.Content = fontFamily;
            listBoxItem.FontFamily=fontFamily; // Shows Font Text in the Font
            FontFamilyListBox.Items.Add(listBoxItem);
        }