June 24, 2011, 1:05 p.m.
posted by soulmaker
Twisting and Turning with Transformations
Normally, anything you draw on the graphics canvas is laid down directly on the bitmap surface. It's like a giant grid, and your drawing commands are basically dropping colored inks directly into each grid cell. The Graphics object also gives you the ability to pass your drawing commands through a geometric transformation before their output goes to the canvas surface. For instance, a rotation transformation would rotate your lines, shapes, and text by the amount you specify (in degrees), and then apply the result to the surface. Figure displays the results of the following code, which applies two translations: (1) moving the (0, 0) origin right by 100 pixels, and down by 75 pixels; and (2) adding a clockwise rotation of 270 degrees.
e.Graphics.DrawString("Normal", _ SystemFonts.DefaultFont, Brushes.Black, 10, 10) e.Graphics.TranslateTransform(100, 75) e.Graphics.RotateTransform(270) e.Graphics.DrawString("Rotated", _ SystemFonts.DefaultFont, Brushes.Black, 10, 10)
Normal and rotated text
Transformations are cumulative; if you apply multiple transformations to the canvas, any drawing commands will pass through all of the transformations before arriving at the canvas. The order in which transformations occur is important. If the code we just ran had reversed the TranslateTransform and RotateTransform statements, the rotation would have altered the x-y coordinates for the entire canvas world. The subsequent translation of (100, 75) would have moved up the origin 100 pixels, and then to the right 75 pixels.
The Graphics class includes these methods that let you apply transformations to the "world view" of the canvas during drawing.