# Rotating Object About Center Using GDI++

• October 26th, 2010, 10:32 PM
mmscg
Rotating Object About Center Using GDI++
I am trying to rotate a bitmap about it's center point.
The bitmap I am using in my test project is 200 X 200 pixels.

By trial and error, I came up with the following. It works, but I don't understand why.
Does anyone understand why this works for the 200 X 200 bmp?
Code:

```myGraphics.TranslateTransform(200,200); myGraphics.RotateTransform(AngleX); myGraphics.TranslateTransform(-100,-100); myGraphics.DrawImage(&image,0,0);```
(I increment AngleX by 10 in WM_LBUTTONDOWN, and in 36 clicks the image comes full circle.)
• October 28th, 2010, 05:13 PM
Chris_F
Re: Rotating Object About Center Using GDI++
Because GDI uses pixel coordinates, and when you rotate something, you rotate it around it's origin (0, 0). The origin in an image is in the top left hand corner. You need to translate transform the image first so that the origin is in the center of the image, rotate, and then translate transform it back to normal before drawing it. The values you use are dependent on the image's dimensions, so you should use something like image.Width and image.Height so that it works on all images regardless of resolution.
• October 28th, 2010, 05:32 PM
Chris_F
Re: Rotating Object About Center Using GDI++
To clarify:

Code:

```myGraphics.TranslateTransform( (Width / 2), (Height / 2) ); myGraphics.RotateTransform(AngleX); myGraphics.TranslateTransform( -(Width / 2), -(Height / 2) ); myGraphics.DrawImage(&image,0,0);```
is how you do it. It's important to do your transformations in the right order, otherwise you won't get the result you want.
• October 30th, 2010, 08:55 AM
mmscg
Re: Rotating Object About Center Using GDI++
Thanks Chris_F

I think I understand what you are saying.

I will try later and see if it works for me like I think it should.
• October 31st, 2010, 08:25 AM
mmscg
Re: Rotating Object About Center Using GDI++
OK, I tried your code and it works perfectly to rotate the image about the center.

It draws the rotating bitmap with top left corner at (0, 0) on my form.

I tried moving the top left corner of the rotating bitmap to a point other than (0, 0), say (100, 100) like so:

Code:

```graphics.TranslateTransform((REAL)(Width / 2), (REAL)(Height /2 )); graphics.RotateTransform(AngleX); graphics.TranslateTransform((REAL) -(Width / 2), (REAL) -(Height / 2)); graphics.DrawImage(&image, 100, 100);```
This doesn't work. (it rotates about the point (0, 0))

I tried a couple of other things, but nothing works.

What am I doing wrong?
• June 27th, 2013, 01:19 PM
pates
Re: Rotating Object About Center Using GDI++
This is way too late for the original author, but I just encountered a similar issue and wanted to post a follow-up for others' benefit.

TranslateTransform() needs the absolute rotation point, not the rotation point relative to the image. So you'd do something like this:

Code:

```const int xPos = 100, yPos = 100; LONG transformX = xPos + Width / 2; LONG transformY = yPos + Height / 2; graphics.TranslateTransform(transformX, transformY); graphics.RotateTransform(AngleX); graphics.TranslateTransform(-transformX, -transformY); graphics.DrawImage(&image, xPos, yPos);```