AdSense

Monday 20 May 2013

C# Windows Forms - Rotate image

(Deutsche Version) For Panzerkampf it is necessary to rotate an image. The solution which I use is:

private Image rotateImage(Image b, double angle)
{
  //create a new empty bitmap to hold rotated image
  Bitmap returnBitmap = new Bitmap(2*b.Width, 2*b.Height);
  //make a graphics object from the empty bitmap
  Graphics g = Graphics.FromImage(returnBitmap);
  //move rotation point to center of image
  g.TranslateTransform(center_x, center_y);
  //rotate
  g.RotateTransform(-(float)(angle*180.0/Math.PI));
  //move image back
  g.TranslateTransform(-center_x, -center_y);
  //draw passed in image onto graphics object
  g.DrawImage(b, new Point(0, 0));
  return returnBitmap;
}

1 comment: