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 flipped vertically, horizontally, or both in Visual Basic .NET
DescriptionThis example shows how to draw text flipped vertically, horizontally, or both in Visual Basic .NET. It uses the Graphics object's Scale method to flip the text. It also does a little math to ensure that the result is properly positioned.
KeywordsGraphics object, Scale, flip, DrawString, text, VB.NET, Visual Basic .NET
CategoriesGraphics, VB.NET
 
Subroutine DrawFlippedText does all of the interesting work. It saves the current graphics state and then sets up a transformation to properly flip the text. It then calculates where it needs to draw the text so it lands in the desired location. The routine then draws the text and restores the original graphics state.
 
Public Sub DrawFlippedText(ByVal gr As Graphics, ByVal _
    the_font As Font, ByVal the_brush As Brush, ByVal x As _
    Integer, ByVal y As Integer, ByVal txt As String, ByVal _
    flip_x As Boolean, ByVal flip_y As Boolean)
    ' Save the current graphics state.
    Dim state As GraphicsState = gr.Save()

    ' Set up the transformation.
    Dim scale_x As Integer = IIf(flip_x, -1, 1)
    Dim scale_y As Integer = IIf(flip_y, -1, 1)
    gr.ResetTransform()
    gr.ScaleTransform(scale_x, scale_y)

    ' Figure out where to draw.
    Dim txt_size As SizeF = gr.MeasureString(txt, the_font)
    If flip_x Then x = -x - txt_size.Width
    If flip_y Then y = -y - txt_size.Height

    ' Draw.
    gr.DrawString(txt, the_font, the_brush, x, y)

    ' Restore the original graphics state.
    gr.Restore(state)
End Sub
 
The main program calls this subroutine as in the following code.
 
DrawFlippedText(e.Graphics, the_font, Brushes.Red, 10, 10, _
    "Flip X", True, False)
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated