0

I have a picturebox with SizeMode=Zoom. I have rectangles (annotations) drawn on the picturebox too.

This is the code that rotates the image in the picturebox (in click event of a button)

Image img = picZoom.Image;
img.RotateFlip(RotateFlipType.Rotate90FlipXY);
picZoom.Image = img;
RotationgAngle += 90;

This, of course, fires off the Paint event, which contains the following:

for (int i = 0; i <= Annotations.Items.Count - 1; i++)
{
     Rectangle r = functions.ZoomRectangle(Annotations.Items[i].Box, picZoom, true);

     using (Matrix m = new Matrix())
     {
          m.RotateAt(RotationgAngle, new PointF(r.Left + (r.Width / 2), r.Top + (r.Height / 2)));

          e.Graphics.Transform = m;
          e.Graphics.FillRectangle(new SolidBrush(c), r);
          e.Graphics.ResetTransform();
     }

     e.Graphics.FillRectangle(new SolidBrush(c), functions.ZoomRectangle(Annotations.Items[i].Box, picZoom, true));
}

The ZoomRectangle function scales the rectangle based on the Zoom level of the picturebox and the size of the image within the PictureBox. The rectangle rotate, just not in the right position.

When I rotate the image in my picturebox, how would I rotate my filled rectangle to maintain the exact same relative position on the picturebox?

  • [Matrix.RotateAt()](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.drawing2d.matrix.rotateat) -- Example usage and related methods to scale a shape using relative positions and zoomed Images: [Zoom and translate an Image from the mouse location](https://stackoverflow.com/a/61964222/7444103) -- You're not *rotating the PictureBox*. I suggest you perform any transformation of an Image in code, in the Paint handler. – Jimi Jun 08 '21 at 17:36
  • Another, simplified, example: [Image.RotateFlip doesn't seem to rotate the Bitmap](https://stackoverflow.com/a/53764409/7444103) -- See also the methods shown here: [Translate Rectangle Position in Zoom Mode Picturebox](https://stackoverflow.com/q/53800328/7444103) – Jimi Jun 08 '21 at 17:43
  • I failed to add my RotateAt code in my original question. The rotated rectangle seems to be the correct size. It is just in the wrong place. – RickInWestPalmBeach Jun 08 '21 at 18:50
  • What all those answers suggest is to draw the Image **scaled** inside a Container (your PictureBox Client Area, in this case). Thus, you also **scale** the Rectangle. Then you rotate the world coordinates. What you draw after the transformation is applied, is all drawn rotated. -- If you need to save the result to an Image, at some point, to save to disc or whatever, derive a Graphics object from a Bitmap and perform he same operation using this Graphics object. -- A description of what this *rotation* procedure is used for wouldn't hurt. – Jimi Jun 08 '21 at 19:31
  • BTW, you cannot initialize and use a Brush like this: `e.Graphics.FillRectangle(new SolidBrush(c), ...);`. You're leaking Graphic resources (and handles). Declare your Graphic objects with `using` statements. – Jimi Jun 08 '21 at 19:34
  • I don't always send all the code in order to minimize what needs to be reviewed with the question. Just enough to get the point across. – RickInWestPalmBeach Jun 09 '21 at 12:13

0 Answers0