How to: Enumerate Installed Fonts

The InstalledFontCollection class inherits from the FontCollection abstract base class. You can use an InstalledFontCollection object to enumerate the fonts installed on the computer. The Families property of an InstalledFontCollection object is an array of FontFamily objects.

The following example lists the names of all the font families installed on the computer. The code retrieves the Name property of each FontFamily object in the array returned by the Families property. As the family names are retrieved, they are concatenated to form a comma-separated list. Then the DrawString method of the Graphics class draws the comma-separated list in a rectangle.

If you run the example code, the output will be similar to that shown in the following illustration.

Installed Fonts
        Dim fontFamily As New FontFamily("Arial")
        Dim font As New Font( _
           fontFamily, _
           8, _
           FontStyle.Regular, _
           GraphicsUnit.Point)
        Dim rectF As New RectangleF(10, 10, 500, 500)
        Dim solidBrush As New SolidBrush(Color.Black)

        Dim familyName As String 
        Dim familyList As String = "" 
        Dim fontFamilies() As FontFamily

        Dim installedFontCollection As New InstalledFontCollection()

        ' Get the array of FontFamily objects.
        fontFamilies = installedFontCollection.Families

        ' The loop below creates a large string that is a comma-separated 
        ' list of all font family names. 
        Dim count As Integer = fontFamilies.Length
        Dim j As Integer 

        While j < count
            familyName = fontFamilies(j).Name
            familyList = familyList & familyName
            familyList = familyList & ",  "
            j += 1
        End While 

        ' Draw the large string (list of all families) in a rectangle.
        e.Graphics.DrawString(familyList, font, solidBrush, rectF)

The preceding example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of PaintEventHandler. In addition, you should import the System.Drawing.Text namespace.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft