Constructing a GDI+ Font object in VB.NET

In this article I will explain about Constructing a Font object in GDI+.
  • 443

A Font Object belongs to the FontFamily class, so before we construct a font object, we need to construct a FontFamily object. The following code snippet creates two FontFamily objects, belonging to the Verdana and Arial font families, respectively.

        'Create font families
        Dim verdanaFamily As New FontFamily("Verdana")
        Dim arialFamily As New FontFamily("Arial")

The Font class provide more than a dozen overloaded constructors, which allow an application to construct a Font object in different ways, either from strings names of a font family and size or from a FontFamily object with font style and optinal GraphicsUnit values.

The Simplest way to create a Font object is to pass the font family name as the first argument and the point size as the second argument of the Font constructor. The following code snippet creates a Times New Roman 12-point font:


Dim tnwFont As New Font("Times New Roman", 12)

The following code snippet creates three fonts in different styles belonging to the Verdana, Tahoma, and Arial font families, respectively.


        'Construct Font objects
        Dim verdanaFamily As New FontFamily("Verdana")
        Dim arialFamily As New FontFamily("Arial")
        Dim VerdanaFont As New Font(verdanaFamily, 14, FontStyle.Regular, GraphicsUnit.Pixel)        Dim tahomaFont As New Font(New FontFamily("Tahoma"), 10, FontStyle.Bold Or FontStyle.Italic, GraphicsUnit.Pixel)
        Dim arialFont As New Font(arialFamily, 16, FontStyle.Bold, GraphicsUnit.Point)
        Dim tnFont As New Font("Times New Roman", 12)

Note: As the code example here shows, you can use the FontStyle and GraphicsUnit enumeration to define the style and units of a font, respectively.

If you don't want to create and use a FontFamily object in constructing a font, you can pass the font family name and size directly when you create a new Font object. The following code snippet creates three fonts from the Verdana, Arial, and Tahoma font families, respectively, with different sizes and styles.


        'Construct Font objects
        Dim verdanaFont As New Font("Verdana", 12)
        Dim arialFont As New Font("Arial", 10)
        Dim tahomaFont As New Font("Arial", 14, FontStyle.Underline Or FontStyle.Italic)

Conclusion

Hope the article would have helped you in understanding Constructing a Font object in GDI+. Read other articles on GDI+ on the website.

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.