GDI+ Transforming Text in VB.NET

In this article I will explain how to Transforming Text in GDI+.
  • 4929
 

Transformation is a process of moving objects from one place to another by applying a series of operations such as scaling, rotation, and translation. In this section we will see how to transform text using the Graphics object.

Transformation using Graphics class methods and properties is pretty simple. The Graphics class provides the methods ScaleTransform, RotateTransform, TranslateTransform and others.

Let's look as a simple yet useful example of text transformation. First we draw some text on a form using the code in Listing 5.18.

LISTING 5.18: Drawing text on a form


Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
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
        Dim g As Graphics = e.Graphics
        Dim str As String = "colors, fonts, and text are common elements" & " of graphics programming. In this article, you learned " & " about the colors, fonts, and text representations in the " & " .NET Framework class library. You learned how to create " & " these elements and use them in GDI+."
        g.ScaleTransform(2, 1)
        g.RotateTransform(45.0F, System.Drawing.Drawing2D.MatrixOrder.Prepend)
        g.TranslateTransform(-20, -70)
        g.DrawString(str, New Font("Verdana", 10), New SolidBrush(Color.Blue), New Rectangle(50, 20, 200, 300))

    End Sub
End Class

Figure 5.20 shows the output of Listing 5.18. The text is drawn normally.

Now let's scale the text using the ScaleTransform method by writing the following line before the DrawString method call. Scaling changes the text size by application of a scaling factor. For example, the following code line doubles the size of text. This code must be added before the DrawString method call:

g.ScaleTransform(2, 1)

Now our text on the form looks like Figure 5.21. It is scaled to twice the regular size.

Figure-5_20.gif

FIGURE 5.20: Drawing text on a form

Figure-5_21.gif

FIGURE 5.21: Using ScaleTransform to scale text

Now let's rotate the text, which we can do by calling the RotateTransform method, which takes a rotation angle. We rotate the text 45 degrees by adding the following line before the DrawString method call:

g.RotateTransform(45F, System.Drawing.Drawing2D.MatrixOrder.Prepend)

Now the text on the form looks like Figure 5.22. It is rotated from its previous position.

Figure-5_22.gif

FIGURE 5.22: Using RotateTransform to rotate text

Figure-5_22.gif

FIGURE 5.23: Using TranslateTransform to translate text

Finally let's call TranslateTransform, which takes two values related to the x- and y-axes. We add the following line after RotateTransform:

g.TranslateTransform(-20, -70)

and our final form looks like Figure 5.23. The text has been moved (or "translated") from its previous position.

Conclusion

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

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.