private Bitmap RotateImage(Image image, float rotateAtX, float rotateAtY, float angle)
{
Rectangle newSize = ImageMaths.GetBoundingRotatedRectangle(image.Width, image.Height, angle);
int x = (newSize.Width - image.Width) / 2;
int y = (newSize.Height - image.Height) / 2;
// Create a new empty bitmap to hold rotated image
Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppPArgb);
newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
// Make a graphics object from the empty bitmap
using (Graphics graphics = Graphics.FromImage(newImage))
{
// Reduce the jagged edge.
GraphicsHelper.SetGraphicsOptions(graphics);
// Put the rotation point in the "center" of the image
graphics.TranslateTransform(rotateAtX + x, rotateAtY + y);
// Rotate the image
graphics.RotateTransform(angle);
// Move the image back
graphics.TranslateTransform(-rotateAtX - x, -rotateAtY - y);
// Draw passed in image onto graphics object
graphics.DrawImage(image, new PointF(x, y));
}
image.Dispose();
return newImage;
}