Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleUse transformations to draw angled text in VB .NET
DescriptionThis example shows how to use transformations to draw angled text in VB .NET. It rotates, stretches, and then rotates back a font.
Keywordsfont, angled text, VB.NET, transformation, Graphics
CategoriesVB.NET, Graphics
 
The DrawAngledText subroutine translates a point to the origin. It rotates and then scales. This stretches text vertically. Because the text is rotated, the stretching makes the font slant at an angle.

Next the program rotates back to the horizontal. The angle it needs to rotate through has a tangent equal to the vertical scale factor times the original angle's Tangent. (Draw a picture and it will be relatively straightforward.)

Finally, the program translates the origin back to the destination point and draws the text.

 
' Draw text at this position, rotated by the indicated
' amount.
Public Sub DrawAngledText(ByVal gr As Graphics, ByVal _
    the_font As Font, ByVal the_brush As Brush, ByVal txt _
    As String, ByVal x As Integer, ByVal y As Integer, _
    ByVal angle_degrees As Single, ByVal y_scale As Single)
    ' Translate the point to the origin.
    gr.TranslateTransform(-x, -y, _
        Drawing2D.MatrixOrder.Append)

    ' Rotate through the angle.
    gr.RotateTransform(angle_degrees, _
        Drawing2D.MatrixOrder.Append)

    ' Scale vertically by a factor of Tan(angle).
    Dim angle_radians As Double = angle_degrees * PI / 180
    gr.ScaleTransform(1, y_scale, _
        Drawing2D.MatrixOrder.Append)

    ' Find the inverse angle and rotate back.
    angle_radians = Math.Atan(y_scale * Tan(angle_radians))
    angle_degrees = CSng(angle_radians * 180 / PI)
    gr.RotateTransform(-angle_degrees, _
        Drawing2D.MatrixOrder.Append)

    ' Translate the origin back to the point.
    gr.TranslateTransform(x, y, _
        Drawing2D.MatrixOrder.Append)

    ' Draw the text.
    gr.TextRenderingHint = _
        TextRenderingHint.AntiAliasGridFit
    gr.DrawString(txt, the_font, the_brush, x, y)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated