GDI+ Rotated sub-image
- by Andrew Robinson
I have a rather large (30MB) image that I would like to take a small "slice" out of. The slice needs to represent a rotated portion of the original image.
The following works but the corners are empty and it appears that I am taking a rectangular area of the original image, then rotating that and drawing it on an unrotated surface resulting in the missing corners.
What I want is a rotated selection on the original image that is then drawn on an unrotated surface. I know I can first rotate the original image to accomplish this but this seems inefficient given its size.
Any suggestions? Thanks,
public Image SubImage(Image image, int x, int y, int width, int height, float angle)
{
var bitmap = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.TranslateTransform(bitmap.Width / 2.0f, bitmap.Height / 2.0f);
graphics.RotateTransform(angle);
graphics.TranslateTransform(-bitmap.Width / 2.0f, -bitmap.Height / 2.0f);
graphics.DrawImage(image, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
}
return bitmap;
}