Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate System.Drawing.Image x degrees?

Tags:

c#

gdi+

I need to rotate my image of a car X degrees to indicate the direction of travel. Right now I have this working code to draw the image on a GDI+ surface.

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

how to rotate X degrees?

like image 777
patrick Avatar asked Dec 05 '25 01:12

patrick


1 Answers

Here's how to rotate an image

 //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);
like image 173
PaulB Avatar answered Dec 07 '25 15:12

PaulB