C# Get all available FontFamily
Asked Answered
J

4

6

I have an input box, and people type a font in and it saves what they type as a JPEG. All works fine. But when they type a font name like 'times new roman' it has to be capitalised properly to 'Times New Roman' or it wont work!

Can I just iterate all the available fonts somehow and present it to them as a dropdown list so there are no spelling problems and they definitely will only be using fonts on the system?

Jordain answered 17/3, 2011 at 17:19 Comment(0)
L
10

Simply use next code:

FontFamily[] ffArray = FontFamily.Families;
foreach (FontFamily ff in ffArray)
{
    //Add ff.Name to your drop-down list
}
Lelialelith answered 17/3, 2011 at 17:22 Comment(1)
Here is a Powershell one liner for convenience, especially if you just want the output: [System.Drawing.FontFamily]::FamiliesHolothurian
K
7

Or you can just bind to it directly:

<ComboBox ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}" />

Kahler answered 17/3, 2011 at 17:29 Comment(0)
G
2

I have font lists in several spots within my application so I like to load the list once and reuse the list to bind to the controls.

public List<string> GetFontFamilies()
{
    List<string> fontfamilies = new List<string>();                                   
    foreach (FontFamily family in FontFamily.Families)
    {
        fontfamilies.Add(family.Name);
    }
    return fontfamilies;       

}
Gruesome answered 17/3, 2011 at 17:45 Comment(0)
N
0

This is pretty much the same as Gary's answer but a little more compact:

public static readonly List<string> FontNames = FontFamily.Families.Select(f => f.Name).ToList();
Naturalistic answered 10/2, 2013 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.