2

I have a bitmap that I need to rotate an arbitrary number of degrees in C#. I need the result to be a bitmap. I see a number of questions partially addressing this problem on StackOverflow. The result for these questions are a rotated image drawn in a System.Drawing.Graphics object and displayed on the screen. That's not what I need. I need the image to be rotated, and placed into a bitmap object.

I see no methods for System.Drawing.Graphics to pull out the underlying bitmap.

Is there another way to get at this? I need at a minimum bilinear interpolation as well.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
John
  • 5,735
  • 3
  • 46
  • 62

2 Answers2

3

You can use the same steps, but instead of the Graphics object being linked to the display, you can have it linked to a new Bitmap object.

Bitmap rotated = new Bitmap(<dimensions>)
using(Graphics g = Graphics.FromImage(rotated))
{
  // Code to draw the rotated image to g goes here
}
itsme86
  • 19,266
  • 4
  • 41
  • 57
  • New to C#, so sorry for the stupid question. The underlying "rotated" image is then modified by g.drawImageUnscaled? And Graphics properly deals with situations where the image to be placed on the graphics object go off the boundary? Very cool. Thanks. – John Mar 15 '12 at 15:39
0

Just in my thought:

  1. You can use the exists image rotation like: http://dotnet-snippets.com/dns/image-rotation-by-angle-SID600.aspx

  2. You need to modify the above code to get the exact boundary: use some simple calculation with sin/cos/tan and you will find the correct

Hope it works :)

Hoa Tran
  • 271
  • 2
  • 6