如何旋转System.Drawing.Image x degrees?

时间:2011-08-23 14:54:47

标签: c# gdi+

我需要将汽车的图像旋转X度以指示行进方向。现在我有这个工作代码在GDI +表面上绘制图像。

int hdc = Display.hDC;
IntPtr p = new IntPtr(hdc);
graphics = Graphics.FromHdc(p);
newImage = Image.FromFile(carImage);
System.Drawing.Point ulCorner = new System.Drawing.Point(x - 25, y -15);

//graphics.RotateTransform(45); //tried this line, when i used it, it drew nothing.
graphics.DrawImage(newImage, ulCorner);

如何旋转X度?

1 个答案:

答案 0 :(得分:4)

以下是旋转图像的方法

 //move rotation point to center of image
  graphics.TranslateTransform((float)newImage.Width/2,(float)newImage.Height / 2);
  //rotate
  graphics.RotateTransform(angle);
  //move image back
  graphics.TranslateTransform(-(float)newImage.Width/2,-(float)newImage.Height / 2);