1

我需要在画布上绘制一定角度的图像,它需要旋转角度 N ,它的中心在 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);

但我发现 img 在左上角旋转,我需要图像以其中心旋转。我怎样才能做到这一点?非常感谢!

4

1 回答 1

2

您需要更改默认为左上角的旋转“中心”。
这是我在网上找到的一些代码:

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;
}
于 2009-04-13T07:51:45.137 回答