zetar
zetar

Reputation: 1233

How do you rotate a bitmap an arbitrary number of degrees?

I have a bitmap:

Bitmap UnitImageBMP

And I need to rotate it an arbitrary number of degrees. How do I do this? The RotateFlip method will only rotate in increments of 90 degrees.

Upvotes: 1

Views: 3333

Answers (1)

Neil Loftin
Neil Loftin

Reputation: 46

I did some searching for you and found this:

public static 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
  using(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;
}

Upvotes: 1

Related Questions