How to: Create a Private Font Collection
The PrivateFontCollection class inherits from the FontCollection abstract base class. You can use a PrivateFontCollection object to maintain a set of fonts specifically for your application. A private font collection can include installed system fonts as well as fonts that have not been installed on the computer. To add a font file to a private font collection, call the AddFontFile method of a PrivateFontCollection object.
The Families property of a PrivateFontCollection object contains an array of FontFamily objects.
The number of font families in a private font collection is not necessarily the same as the number of font files that have been added to the collection. For example, suppose you add the files ArialBd.tff, Times.tff, and TimesBd.tff to a collection. There will be three files but only two families in the collection because Times.tff and TimesBd.tff belong to the same family.
Example
The following example adds the following three font files to a PrivateFontCollection object:
C:\systemroot\Fonts\Arial.tff (Arial, regular)
C:\systemroot\Fonts\CourBI.tff (Courier New, bold italic)
C:\systemroot\Fonts\TimesBd.tff (Times New Roman, bold)
The code retrieves an array of FontFamily objects from the Families property of the PrivateFontCollection object.
For each FontFamily object in the collection, the code calls the IsStyleAvailable method to determine whether various styles (regular, bold, italic, bold italic, underline, and strikeout) are available. The arguments passed to the IsStyleAvailable method are members of the FontStyle enumeration.
If a given family/style combination is available, a Font object is constructed using that family and style. The first argument passed to the Font constructor is the font family name (not a FontFamily object as is the case for other variations of the Font constructor). After the Font object is constructed, it is passed to the DrawString method of the Graphics class to display the family name along with the name of the style.
The output of the following code is similar to the output shown in the following illustration:
Arial.tff (which was added to the private font collection in the following code example) is the font file for the Arial regular style. Note, however, that the program output shows several available styles other than regular for the Arial font family. That is because GDI+ can simulate the bold, italic, and bold italic styles from the regular style. GDI+ can also produce underlines and strikeouts from the regular style.
Similarly, GDI+ can simulate the bold italic style from either the bold style or the italic style. The program output shows that the bold italic style is available for the Times family even though TimesBd.tff (Times New Roman, bold) is the only Times file in the collection.
// Helper function to print text in a font and style
private float DrawFont(Graphics graphicsObj,
FontFamily family,
FontStyle style,
SolidBrush colorBrush,
PointF location,
string styleName)
{
// The string to print, which contains the family name and style
string familyNameAndStyle = $"{family.Name} {styleName}";
// Create the font object
using (Font fontObject = new Font(family.Name, 16, style, GraphicsUnit.Pixel))
{
// Draw the string
graphicsObj.DrawString(familyNameAndStyle, fontObject, colorBrush, location);
// Return the height of the font
return fontObject.Height;
}
}
// The OnPaint method of a form, which provides the graphics object
protected override void OnPaint(PaintEventArgs e)
{
PointF location = new PointF(10, 0);
SolidBrush solidBrush = new SolidBrush(Color.Black);
FontFamily[] fontFamilies;
PrivateFontCollection privateFontCollection = new PrivateFontCollection(); // Dispose later
// Add three font files to the private collection.
privateFontCollection.AddFontFile(System.Environment.ExpandEnvironmentVariables("%systemroot%\\Fonts\\Arial.ttf"));
privateFontCollection.AddFontFile(System.Environment.ExpandEnvironmentVariables("%systemroot%\\Fonts\\CourBI.ttf"));
privateFontCollection.AddFontFile(System.Environment.ExpandEnvironmentVariables("%systemroot%\\Fonts\\TimesBD.ttf"));
// Get the array of FontFamily objects.
fontFamilies = privateFontCollection.Families;
// Process each font in the collection
for (int i = 0; i < fontFamilies.Length; i++)
{
// Draw the font in every style
// Regular
if (fontFamilies[i].IsStyleAvailable(FontStyle.Regular))
location.Y += DrawFont(e.Graphics, fontFamilies[i], FontStyle.Regular, solidBrush, location, "Regular");
// Bold
if (fontFamilies[i].IsStyleAvailable(FontStyle.Bold))
location.Y += DrawFont(e.Graphics, fontFamilies[i], FontStyle.Bold, solidBrush, location, "Bold");
// Italic
if (fontFamilies[i].IsStyleAvailable(FontStyle.Italic))
location.Y += DrawFont(e.Graphics, fontFamilies[i], FontStyle.Italic, solidBrush, location, "Italic");
// Bold and Italic
if (fontFamilies[i].IsStyleAvailable(FontStyle.Bold) &&
fontFamilies[i].IsStyleAvailable(FontStyle.Italic))
location.Y += DrawFont(e.Graphics, fontFamilies[i], FontStyle.Bold | FontStyle.Italic, solidBrush, location, "BoldItalic");
// Underline
if (fontFamilies[i].IsStyleAvailable(FontStyle.Underline))
location.Y += DrawFont(e.Graphics, fontFamilies[i], FontStyle.Underline, solidBrush, location, "Underline");
// Strikeout
if (fontFamilies[i].IsStyleAvailable(FontStyle.Strikeout))
location.Y += DrawFont(e.Graphics, fontFamilies[i], FontStyle.Strikeout, solidBrush, location, "Strikeout");
// Extra space between font families
location.Y += 10;
}
privateFontCollection.Dispose();
}
' Helper function to print text in a font and style
Private Function DrawFont(graphicsObj As Graphics,
family As FontFamily,
style As FontStyle,
colorBrush As SolidBrush,
location As PointF,
styleName As String) As Single
' The string to print, which contains the family name and style
Dim familyNameAndStyle As String = $"{family.Name} {styleName}"
' Create the font object
Using fontObject As New Font(family.Name, 16, style, GraphicsUnit.Pixel)
' Draw the string
graphicsObj.DrawString(familyNameAndStyle, fontObject, colorBrush, location)
' Return the height of the font
Return fontObject.Height
End Using
End Function
' The OnPaint method of a form, which provides the graphics object
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim location As New PointF(10, 0)
Dim solidBrush As New SolidBrush(Color.Black)
Dim fontFamilies() As FontFamily
Dim privateFontCollection As New PrivateFontCollection() ' Dispose later
' Add three font files to the private collection.
privateFontCollection.AddFontFile(System.Environment.ExpandEnvironmentVariables("%systemroot%\Fonts\Arial.ttf"))
privateFontCollection.AddFontFile(System.Environment.ExpandEnvironmentVariables("%systemroot%\Fonts\CourBI.ttf"))
privateFontCollection.AddFontFile(System.Environment.ExpandEnvironmentVariables("%systemroot%\Fonts\TimesBD.ttf"))
' Get the array of FontFamily objects.
fontFamilies = privateFontCollection.Families
' Process each font in the collection
For i = 0 To fontFamilies.Length - 1
' Draw the font in every style
' Regular
If fontFamilies(i).IsStyleAvailable(FontStyle.Regular) Then
location.Y += DrawFont(e.Graphics, fontFamilies(i), FontStyle.Regular, solidBrush, location, "Regular")
End If
' Bold
If fontFamilies(i).IsStyleAvailable(FontStyle.Bold) Then
location.Y += DrawFont(e.Graphics, fontFamilies(i), FontStyle.Bold, solidBrush, location, "Bold")
End If
' Italic
If fontFamilies(i).IsStyleAvailable(FontStyle.Italic) Then
location.Y += DrawFont(e.Graphics, fontFamilies(i), FontStyle.Italic, solidBrush, location, "Italic")
End If
' Bold and Italic
If fontFamilies(i).IsStyleAvailable(FontStyle.Italic) And
fontFamilies(i).IsStyleAvailable(FontStyle.Italic) Then
location.Y += DrawFont(e.Graphics, fontFamilies(i), FontStyle.Bold Or FontStyle.Italic, solidBrush, location, "BoldItalic")
End If
' Underline
If fontFamilies(i).IsStyleAvailable(FontStyle.Underline) Then
location.Y += DrawFont(e.Graphics, fontFamilies(i), FontStyle.Underline, solidBrush, location, "Underline")
End If
' Strikeout
If fontFamilies(i).IsStyleAvailable(FontStyle.Strikeout) Then
location.Y += DrawFont(e.Graphics, fontFamilies(i), FontStyle.Strikeout, solidBrush, location, "Strikeout")
End If
' Extra space between font families
location.Y += 10
Next
privateFontCollection.Dispose()
End Sub
Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires PaintEventArgs e
, which is a parameter of PaintEventHandler.
See also
.NET Desktop feedback