Working with Text and Strings in VB.NET

In this article I will explain about Working with Text and Strings in GDI+.
  • 6147
 

As we discussed, the DrawString method of the Graphics class can be used to draw text on a graphics surface. The DrawString method takes a string, font, brush and starting point.

Listing 5.8 creates three different fonts and draws text on a form using the DrawString method. Each DrawString method uses a different color and font to draw the string.
 
LISTING 5.8: Drawing text on a graphics surface

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.IO
Imports System.Text
Imports System.Windows.Forms
Public Class Form1

    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        'Create a Graphics object
        Dim g As Graphics = Me.CreateGraphics()
        'Create font families
        Dim verdanaFamily As New FontFamily("Verdana")
        Dim arialFamily As New FontFamily("Arial")

        'Construct font objects
        Dim verdanaFont As New Font("Verdana", 10)
        Dim arialFont As New Font(arialFamily, 16, FontStyle.Bold)
        Dim tahomaFont As New Font("Tahoma", 24, FontStyle.Underline Or FontStyle.Italic)

        'Create Brush and other objects
        Dim pointF As New PointF(30, 10)
        Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))
        'Draw text using DrawString
        g.DrawString("Drawing Text", verdanaFont, New SolidBrush(Color.Red), New PointF(20, 20))
        g.DrawString("Drawing Text", arialFont, New SolidBrush(Color.Blue), New PointF(20, 50))
        g.DrawString("Drawing Text", tahomaFont, New SolidBrush(Color.Green), New Point(20, 80))
        'Dispose of objects
        solidBrush.Dispose()
        g.Dispose()
    End Sub
End Class

 
Figure-5_13.gif
 
FIGURE 5.13: Fonts with different styles and sizes
 
Figure 5.13 shows the output from Listing 5.8. The first text is 10-point Verdana; the second, 14-point Arial Bold; and the third, 24-point Tahoma Italic.
 
Conclusion
 
Hope the article would have helped you in understanding Working with Text and Strings in GDI+. Read other articles on GDI+ on the website.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.