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
 
 
 
 
 
 
TitleDraw text that is rotated but otherwise untransformed at a transformed location in VB .NET
DescriptionThis example shows how to draw text that is rotated but otherwise untransformed at a transformed location in VB .NET. The program uses the Graphics object's transformation methods to define the transformation. It then uses the transformation's TransformPoints method to find the transformed location.
Keywordstransform, transformation, text, font, VB.NET
CategoriesGraphics, VB.NET
 
The program's Paint event handler uses the Graphics object's transformation methods to define the transformation. It then draws a transformed rectangle and ellipses.

The Graphics object's Transform property returns the transformation. The code uses this object's TransformPoints method to see where the point (0, 0) is mapped by the transformation.

Next the program resets the transformation to remove it and makes a new transformation that rotates the text and translates it to the desired transformed target location. It then uses DrawString to draw the untransformed text rotated at the transformed location.

 
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    ' Draw a transformed rectangle and some ellipses.
    e.Graphics.ScaleTransform(1.5, 1.5, MatrixOrder.Append)
    e.Graphics.TranslateTransform(80, 20)
    e.Graphics.RotateTransform(20, MatrixOrder.Append)

    e.Graphics.DrawRectangle(Pens.Blue, 0, 0, 100, 100)
    e.Graphics.DrawEllipse(Pens.Red, -3, -3, 6, 6)
    e.Graphics.DrawEllipse(Pens.Yellow, 97, 97, 6, 6)

    ' Draw untransformed text starting at the
    ' transformed location (0, 0).

    ' Transform the point (0, 0) to see where it goes.
    Dim pts() As Point = {New Point(0, 0)}
    e.Graphics.Transform.TransformPoints(pts)

    ' Make a transformation that draws rotated text
    ' translated to this point.
    ' Reset the transformation.
    e.Graphics.ResetTransform()
    ' Rotate.
    e.Graphics.RotateTransform(20, MatrixOrder.Append)
    ' Translate to the target point's transformed location.
    e.Graphics.TranslateTransform(pts(0).X, pts(0).Y, _
        MatrixOrder.Append)

    ' Draw the text at (0, 0).
    Dim the_font As New Font("Times New Roman", 20, _
        FontStyle.Regular, GraphicsUnit.Pixel)
    e.Graphics.DrawString("WYSIWYG", the_font, _
        Brushes.Brown, 0, 0)
    the_font.Dispose()
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated