views:

145

answers:

1

I need to draw an image with a certain angle on a canvas, it need to rotate angle N , and its center is on x, y

        Matrix myPathMatrix;
  myPathMatrix.Translate(x, y, MatrixOrderAppend);
  myPathMatrix.Rotate(angle, MatrixOrderAppend);
  canvas->SetTransform(&myPathMatrix);
  Draw(canvas);// draw the image
  myPathMatrix.Rotate(-angle, MatrixOrderAppend);
  myPathMatrix.Translate(-x, -y, MatrixOrderAppend);
  canvas->SetTransform(&myPathMatrix);

But I find the img rotate by the top left corner, I need the image rotate with its center. How can I do this? many thanks!

+2  A: 

You need to change rotation "center" which is by default top left.
Here some code I found on the net:

private Bitmap rotateImage(Bitmap b, float angle)
{
  //create a new empty bitmap to hold rotated image
  Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
  //make a graphics object from the empty bitmap
  Graphics g = Graphics.FromImage(returnBitmap);
  //move rotation point to center of image
  g.TranslateTransform((float)b.Width/2, (float)b.Height / 2);
  //rotate
  g.RotateTransform(angle);
  //move image back
  g.TranslateTransform(-(float)b.Width/2,-(float)b.Height / 2);
  //draw passed in image onto graphics object
  g.DrawImage(b, new Point(0, 0)); 
  return returnBitmap;
}
Shay Erlichmen
Thank you very much!