GDI+ Rotated sub-image
Posted
by
Andrew Robinson
on Stack Overflow
See other posts from Stack Overflow
or by Andrew Robinson
Published on 2011-02-16T23:20:53Z
Indexed on
2011/02/16
23:25 UTC
Read the original article
Hit count: 277
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;
}
© Stack Overflow or respective owner