GDI+ Transforming Text in VB.NET
In this article I will explain how to Transforming Text in GDI+.
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
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.DrawString(str, New Font("Verdana", 10), New SolidBrush(Color.Blue), New Rectangle(50, 20, 200, 300))
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:
Now our text on the form looks like Figure 5.21. It is scaled to twice the regular size.
FIGURE 5.20: Drawing text on a form
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:
Now the text on the form looks like Figure 5.22. It is rotated from its previous position.
FIGURE 5.22: Using RotateTransform to rotate text
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:
and our final form looks like Figure 5.23. The text has been moved (or "translated") from its previous position.
Hope the article would have helped you in understanding how to Transforming Text in GDI+. Read other articles on GDI+ on the website.