Constructing a Font object in GDI+



This article has been excerpted from book "Graphics Programming with GDI+".

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
            FontFamily verdanaFamily = new FontFamily("Verdana");

            FontFamily arialFamily = 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:


            Font tnwFont = 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
        FontFamily verdanaFamily = new FontFamily("Verdana");
        FontFamily arialFamily = new FontFamily("Arial");
        Font VerdanaFont = new Font (verdanaFamily, 14,
        FontStyle.Regular, GraphicsUnit.Pixel);
        Font tahomaFont = new Font (new FontFamily ("Tahoma"), 10,
        FontStyle.Bold|FontStyle.Italic, GraphicsUnit.Pixel);
        Font arialFont = new Font (arialFamily, 16, FontStyle.Bold,
        GraphicsUnit.Point);
        Font tnFont = 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

Font
verdanaFont = new Font ("Verdana", 12);
Font
arialFont = new Font ("Arial", 10);
Font
tahomaFont = new Font ("Arial", 14, FontStyle.Underline | 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.

bookGDI.jpg
This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.