Fonts in WPF and WinForms

Programming C#

HowTo: Check if a specific Font is installed

The MSDN tells us:

If the familyName parameter specifies a font that is not installed on the machine running the application or is not supported, Microsoft Sans Serif will be substituted.

With that knowlege in mind it's quite easy to check if a certain Font is installed on the system:

private bool IsFontInstalled(string fontName)
{
using (var testFont = new Font(fontName, 8))
{
return 0 == string.Compare(
fontName,
testFont.Name,
StringComparison.InvariantCultureIgnoreCase);
}
}

HowTo: Get a List of all installed Windows Fonts

To retrive a list of all installed Fonts we can simply use the InstalledFontsCollection class of the System.Drawing.Text namespace:

using System.Drawing.Text;

private List<string> GetAllFonts()
{
List<string> lstFonts = new List<string>();

var fontsCollection = new InstalledFontCollection();
foreach (var fontFamily in fontsCollection.Families)
lstFonts.Add(fontFamily.Name);

return lstFonts;
}

HowTo: Use custom Fonts within your WPF Application

For WinForms applications you'll have to include the TTF Fonts within your setup. While this is also a possibility for WPF applications, you could also include them like this:

  1. Add a /Fonts folder to your solution.
  2. Add the True Type Fonts (*.ttf) files to that order
  3. Include the files to the project
  4. Set BuildAction: Content and Copy To Output Directory: Copy if newer. Your .csproj file should now should have a section like this one:
 <ItemGroup>
<Content Include="Fonts\NotoSans-Bold.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Fonts\NotoSans-BoldItalic.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Fonts\NotoSans-Italic.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Fonts\NotoSans-Regular.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Fonts\NotoSansSymbols-Regular.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

5.  In App.xaml add <FontFamily> Resources. It should look like in the following code sample. Note that the URI doesn't contain the filename when packing with the application.

<Applicaton ...>
<Application.Resources>
<FontFamily x:Key="NotoSansRegular">./Fonts/NotoSans-Regular.tts#Noto Sans</FontFamily>
<FontFamily x:Key="NotoSansItalic">./Fonts/NotoSans-Italic.tts#Noto Sans</FontFamily>
<FontFamily x:Key="NotoSansBold">./Fonts/NotoSans-Bold.tts#Noto Sans</FontFamily>
<FontFamily x:Key="NotoSansBoldItalic">./Fonts/NotoSans-BoldItalic.tts#Noto Sans</FontFamily>
<FontFamily x:Key="NotoSansSymbols">./Fonts/NotoSans-Regular.tts#Noto Sans Symbols</FontFamily>
</Application.Resources>
</Application>

6.  Apply your Fonts like this in XAML:

<TextBlock Text="foobar" FontFamily="{StaticResource NotoSansRegular}" 
FontSize="10.0" FontStyle="Normal" FontWeight="Regular" />

For more details visit the MSDN documentation.