ARTICLE

Constructing a Font object in GDI+

Posted by Dinesh Beniwal Articles | GDI+ in VB.NET February 04, 2010
In this article I will explain about Constructing a Font object in GDI+.
 
Reader Level:

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.

share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor