Thread: GDI+ Transforms
View Single Post
Old 08-13-2004, 08:03 AM
Iceplug's Avatar
Iceplug Iceplug is offline

Retired Moderator
* Guru *
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Cool GDI+ Transforms

The Transforms of the GDI are a very powerful way to manipulate your images.
You can have them spin, shrink, and warp in any way you want them to.

There are also the similar Matrix transforms that you can get when you apply a matrix to the Graphics object transform.

Just looking at the the graphics object transforms today, there are four Transform subroutines of the Graphics object that will be of the most interest:

GFX.TranslateTransform(dx, dy)
GFX.ScaleTransform(sx, sy)

The Graphics object that has been created is aimed at something... typically a picturebox or a form, although an image is fine as well.
Now, each of the transforms will move this Graphics object *aim* in several ways, according to the transform.

The aim begins and resets at the upper left corner 0,0.
This is where the aim will be after you have called ResetTransform.
The crosshairs represent where 0,0 is, although you can't see them in the picture.
GFX.DrawImage(cubec, 0, 0)
Let's say that this draws the blue cube (the upper left corner will indicate where the imaginary crosshairs are when you are drawing in VB.NET).

GFX.TranslateTransform(dx, dy)
This one lets you move the point where 0,0 is. Although, it isn't terribly useful by itself (as you can tell where you want to draw the image), it works well with other transforms to make combination effects (discussed later).
The 1st figure below represents a TranslateTransform(40, 32), where the crosshair moves 40 pixels left, and 32 pixels down, and respectively the image will be drawn here afterwards.

Note that if you draw the image before you translatetransform, the image will still be located in the upper left corner (0, 0) of the form or picbox.
Drawing after the translatetransform makes the image appear at 40 32 on the form/picbox.

GFX.ScaleTransform(sx, sy)
This one, more useful than Translate, lets you stretch the aim in the X and Y direction by a specified multiplier. (2f means doubled, 3f means tripled, -1f means flipped over)
A ScaleTransform(2f, 2f) will simply 2x the image, while ScaleTransform(2f, 1f) will make the image 'double-wide', but the same height.
If any parameter is zero, the image will disappear. Using(-1f, 1f) will flip the image horizontally. Using(1f, -1f) will flip it vertically. And Using(-1f, -1f), a double flip, will flip it horizontally and vertically, equivalent to a 180 degree rotate (more on rotation coming soon).
My f's that appear after the number represent singles (floats)... use these so your result is interpreted as a single.
The 2nd figure below represents
GFX.ScaleTransform(0.5f, 1.5f)

Note that if you don't reset the transform and use the same graphics object, the transforms will multiply together, meaning that the picture will be translated and then scaled, as in the 3rd figure.

My favorite. This lets you rotate the aim (clockwise, unless you've flipped it with a ScaleTransform) by an amount in degrees. Notice that it is in degrees... not radians.
The 4th figure below represents
(And Paint won't let me rotate by 45 degrees... hopefully it's obvious though--the picture is rotated clockwise around the crosshair by 45 degrees)

Once you get these, you should be able to make lots of neat effects.
Remember to reset the transform if you don't want the transforms to multiply, and have fun.

Hint: To draw a picture centered on the crosshair:
GFX.DrawImage(bmp, -bmp.Width \ 2, -bmp.Height \ 2)

As usual, if you are using 7.0, the .vb file is all you need.
Attached Images
File Type: gif t2.gif (1.8 KB, 131 views)
File Type: gif t3.gif (1.8 KB, 89 views)
File Type: gif t4.gif (1.9 KB, 78 views)
File Type: gif t5.gif (1.8 KB, 82 views)
Attached Files
File Type: zip (8.1 KB, 339 views)

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote