1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
zetar
  • 1,225
  • 2
  • 20
  • 45

1 Answers1

1

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;
}
  • This may work, but there are some problems. First, if the bitmap image is a rectangle, the new bitmap image will truncate the image. This, presumably, can be solved by making the new bitmap a square of the largest side. However, even after doing this I'm experiencing clipping. I would post a picture if I could. – zetar Apr 26 '16 at 17:47
  • I solved the problem, in a way. I simply made my original bitmap image a square (with extra white space). The RotateImage code now works properly and I blit the transformed image with white as transparent. – zetar Apr 26 '16 at 18:15
  • What a lazy answer! You copied and pasted this code from here: https://stackoverflow.com/a/12025915/1487529 – Elmue Aug 23 '18 at 00:17
  • you say lazy answer, i say lazy question -- i at least connected the asker to the other source. though i admit i should have linked to it. – Neil Loftin Feb 26 '20 at 15:48