I have a bitmap with a Rectangle object that is drawn on top of it. I want to be able to RotateFlip the bitmap and adjust the x, y, width, and height of the Rectangle so that it lines up with the bitmap after each rotation or flip.
For example, if I have a bitmap of 1000 x 800 pixels, I may have a Rectangle object being drawn on it with a specified point and size.
Sample code:
// A bitmap that's 1000x800 size
Bitmap bitmap = new Bitmap(fileName);
// Any arbitrary rectangle that can be drawn inside the bitmap boundaries
Rectangle rect = new Rectangle(200, 200, 100, 100);
bitmap.RotateFlip(rotateFlipType);
switch (rotateFlipType)
{
case Rotate90FlipNone:
// Adjust rectangle to match new bitmap orientation
rect = new Rectangle(?, ?, ?, ?);
break;
case RotateNoneFlip180:
rect = new Rectangle(?, ?, ?, ?);
break;
// ... etc.
}
I find it easiest to reason through each scenario by drawing a picture and labeling
rect.Top,rect.Bottom,rect.Left, andrect.Right. Once that’s done, I either mentally rotate the picture, or even physically rotate the paper. From there, it’s as simple as figuring out where the newrect.Leftandrect.Toplive.A few general tips:
rect.Widthandrect.Heighthave to be swapped.bitmap.Width-rect.Rightandbitmap.Height-rect.Bottom.Here are your two examples with the blanks filled in to get you started: