ARTICLE

Drawing Text in GDI+

Posted by Dinesh Beniwal Articles | GDI+ in VB.NET November 19, 2009
In this article I will explain you how to drawing Text in GDI+.
Download Files:
 
Reader Level:

Drawing Text

This section briefly discusses the drawing of text.

The DrawString method draws a text string on a graphics surface. It has many overloaded forms. Drawstring takes arguments that identify the text, font, brush, starting location, and string format.

Listing 3.6 uses the DrawString method to draw "Hello GDI+ World!" on a form.

LISTING 3.6: Drawing text

    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        e.Graphics.DrawString("Hello GDI+ World!", New Font("Verdana", 16), New SolidBrush(Color.Red), New Point(20, 20))
    End Sub

Note:
You might notice in Listing 3.6 that we create Font, SolidBrush, and Point objects directly as parameters of the DrawString method. This method of creating objects means that we can't dispose of these objects, so some cleanup is left for the garbage collector.

Figure 3.7 shows the output from Listing 3.6


fig3.7.gif

FIGURE 3.7: Drawing text

Now let's see another example of drawing text- this time using the StringFormat class, which defines the text format. Using StringFormat, you can set flags, alignment, trimming, and other options for the text. Listing 3.7 shows different ways to draw text on a graphics surface. In this example the FormatFlags property is set to StringFormatFlags.DirectionVertical, which draws a vertical text.

LISTING 3.7: Using DrawString to draw text on a graphics surface

    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        ' create brushes
        Dim blueBrush As New SolidBrush(Color.Blue)
        Dim redBrush As New SolidBrush(Color.Red)
        Dim greenBrush As New SolidBrush(Color.Green)
        ' Create a rectangle
        Dim rect As New Rectangle(20, 20, 200, 100)

        ' The text to be drawn
        Dim DrawString As [String] = "Hello GDI+ World!"

        ' Create a Font object
        Dim DrawFont As New Font("Verdana", 14)
        Dim x As Single = 100.0F
        Dim y As Single = 100.0F
        Dim DrawFormat As New StringFormat()

        ' Set string format flag to direction vertical,
        ' which draws text vertically
        DrawFormat.FormatFlags = StringFormatFlags.DirectionVertical

        ' Draw string
        e.Graphics.DrawString("Drawing text", New Font("Tahoma", 14), greenBrush, rect)
        e.Graphics.DrawString(DrawString, New Font("Arial", 12), redBrush, 120, 140)
        e.Graphics.DrawString(DrawString, DrawFont, blueBrush, x, y, DrawFormat)

        ' Dispose of objects
        blueBrush.Dispose()
        redBrush.Dispose()
        greenBrush.Dispose()
        DrawFont.Dispose()
    End Sub

Figure 3.8 shows the output from Listing 3.7

fig3.8.gif

FIGURE 3.8: Drawing text with different directions

Conclusion

Hope the article would have helped you in understanding drawing Text in GDI+. Read other articles on GDI+ on the website.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    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.
Team Foundation Server Hosting
Become a Sponsor