Recipe 9.19. Mirroring Text on the Canvas


You want to mirror the text displayed on a graphics canvas.


Sample code folder: Chapter 09\MirrorText

Use a custom matrix transformation through the Graphics object's transform property. This recipe's sample code mirrors text both vertically and horizontally.


Create a new Windows Forms application, and add the following controls to Form1:

  • A RadioButton control named VerticalMirror Set its Text property to Vertical and its Checked property to true.

  • A RadioButton control named HorizontalMirror. Set its Text property to Horizontal.

  • A PictureBox control named MirroredText. Set its BorderStyle property to FixedSingle and its BackColor property to White. Size it so that it can show a sentence or two of text in either direction.

Figure 9-28 shows the layout of the controls on this form.

Figure 9-28. The controls on the mirror text sample

Now add the following source code to Form1's class template:

 Private Const QuoteText As String = _    "The best car safety device is a rear-view mirror " & _    "with a cop in it. (Dudley Moore)" Private Sub VerticalMirror_CheckedChanged( _       ByVal sender As System.Object, _         ByVal e As System.EventArgs) _       Handles VerticalMirror.CheckedChanged    ' ----- Update the display. This event indirectly    '       handles both radio buttons.    MirroredText.Invalidate() End Sub Private Sub MirroredText_Paint(ByVal sender As Object, _       ByVal e As System.Windows.Forms.PaintEventArgs) _       Handles MirroredText.Paint    ' ----- Draw the text and its reverse.    Dim drawingArea As Rectangle    Dim saveState As Drawing2D.GraphicsState    Dim mirrorMatrix As Drawing2D.Matrix    ' ----- Clear the background.    e.Graphics.Clear(Color.White)    ' ----- Deterine the drawing area.    If (VerticalMirror.Checked = True) Then       ' ----- Put text on the left and right of the mirror.       drawingArea = New Rectangle(5, 5, _          (MirroredText.ClientRectangle.Width \ 2) - 10, _          MirroredText.ClientRectangle.Height - 10)       ' ----- Draw the mirror line.       e.Graphics.DrawLine(Pens.Black, _          MirroredText.ClientRectangle.Width \ 2, _          5, MirroredText.ClientRectangle.Width \ 2, _          MirroredText.ClientRectangle.Height - 10)    Else       ' ----- Put text on the top and bottom of the mirror.       drawingArea = New Rectangle(5, 5, _          MirroredText.ClientRectangle.Width - 10, _          (MirroredText.ClientRectangle.Height \ 2) - 10)       ' ----- Draw the mirror line.       e.Graphics.DrawLine(Pens.Black, 5, _          MirroredText.ClientRectangle.Height \ 2, _          MirroredText.ClientRectangle.Width - 10, _          MirroredText.ClientRectangle.Height \ 2)    End If    ' ----- Draw the text.    e.Graphics.DrawString(QuoteText, MirroredText.Font, _       Brushes.Black, drawingArea)    ' ----- Mirror the display.    saveState = e.Graphics.Save()    If (VerticalMirror.Checked = True) Then       mirrorMatrix = New Drawing2D.Matrix(-1, 0, 0, 1, _          MirroredText.ClientRectangle.Width, 0)    Else       mirrorMatrix = New Drawing2D.Matrix(1, 0, 0, -1, _          0, MirroredText.ClientRectangle.Height)    End If    e.Graphics.Transform = mirrorMatrix    ' ----- Draw the text, this time, mirrored.    e.Graphics.DrawString(QuoteText, MirroredText.Font, _       Brushes.Black, drawingArea)    ' ----- Undo the mirror.    e.Graphics.Restore(saveState) End Sub 

Run the program, and use the RadioButton controls to adjust the direction of the mirror. Figure 9-29 shows the mirror in the vertical orientation.

Figure 9-29. Text reversed with a vertical mirror

The Graphics object includes methods that perform basic scaling ( ScaleTransform())), repositioning ( TranslateTransform()), and rotating transformations ( RotateTransform())). While these transformations all seem quite different from each other, they all actually use the same method to accomplish the canvas-level adjustments. Each method sets up a matrix transformation, a mathematical construct that maps points in one coordinate system to another through a basic set of operations. In college-level math courses, this system generally appears under the topic of Linear Algebra.

In addition to the predefined transformations, you can define your own matrix calculation to transform the output in any way you need. This recipe's sample code applies a custom matrix that reverses all coordinate system points in either the horizontal or vertical direction. The intricacies of matrix transformations and cross products are beyond the scope of this book. You can find some basic discussions of the math involved by searching for "matrix transformations" in the Visual Studio online help.

Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: