CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2004
    Posts
    204

    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.)

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    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.
    Last edited by Chris_F; October 28th, 2010 at 05:15 PM.

  3. #3
    Join Date
    Aug 2008
    Posts
    902

    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.
    Last edited by Chris_F; October 28th, 2010 at 05:35 PM.

  4. #4
    Join Date
    Apr 2004
    Posts
    204

    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.

  5. #5
    Join Date
    Apr 2004
    Posts
    204

    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?

  6. #6
    Join Date
    Apr 2005
    Location
    Eden Prairie, MN
    Posts
    18

    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);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured