I just went through a bunch of stuff trying to figure out how to get the image to even rotate. That works but now it's clipping and I'm not really sure how to make it stop... I'm using this rotateImage method:
public static Image RotateImage(Image img, float rotationAngle)
{
//create an empty Bitmap image
Bitmap bmp = new Bitmap(img.Width, img.Height);
//turn the Bitmap into a Graphics object
Graphics gfx = Graphics.FromImage(bmp);
//now we set the rotation point to the center of our image
gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);
//now rotate the image
gfx.RotateTransform(rotationAngle);
gfx.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);
//set the InterpolationMode to HighQualityBicubic so to ensure a high
//quality image once it is transformed to the specified size
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
//now draw our new image onto the graphics object
gfx.DrawImage(img, new System.Drawing.Point(0, 0));
//dispose of our Graphics object
gfx.Dispose();
//return the image
return bmp;
}
I tried making the empty bitmap larger but that only works for one side since the image is pinned to the upper left corner of the bitmap. Any ideas would be appreciated!
I found some help from another website. Here's what I ended up doing for those of you who want to know:
A rotated image may require a larger bitmap to be contained without clipping. A fairly simple way to compute the bounds is to apply the transformation matrix to the corners of the original bounding rectangle. Since the new bitmap is larger, the original image must be drawn such that it is centered, rather than at (0,0).
This is demonstrated in the following revision to your code:
follow these steps:
Details to accomplish above steps:
W,H = width & height of destination
w,h = width & height of source
c=|cos(theta)|
s=|sin(theta)|
w * c + h * s = W
w * s + h * c = H
ox = ( W - w ) / 2
oy = ( H - h ) / 2