Graphics question

  • Thread starter Thread starter Jon
  • Start date Start date
J

Jon

Hello all,

I'm drawing a rectangle on a Form with the below code:

Graphics g = Graphics.FromImage(imgBox.Image);
g.DrawRectangle(new Pen(Color.Red, 1), startX, startY, width, height);

This works fine and draws the rectangle. My questions is, is it
possible to somehow then remove this rectangle?

Thanks all.

JY
 
You could draw it again with the background color, assuming you have not
drawn over it in the mean time with some other geometry.
 
Jon said:
Hello all,

I'm drawing a rectangle on a Form with the below code:

Graphics g = Graphics.FromImage(imgBox.Image);
g.DrawRectangle(new Pen(Color.Red, 1), startX, startY, width, height);

This works fine and draws the rectangle. My questions is, is it
possible to somehow then remove this rectangle?

The rectangle, as you have drawn it, doesn't exist as a *thing*. If it's
drawn on a solid background, then you can draw another rectangle at the
same location in the background color, but that's about it.

If you have multiple shapes on your canvas and you encapsulate them in
classes and store a collection of objects representing the shapes that
you draw on the canvas, then you can create an Erase method for the
collection that redraws the shape in the background color AND then
causes each of the other shapes that it overlapped to redraw itself.
 
The rectangle, as you have drawn it, doesn't exist as a *thing*. If it's
drawn on a solid background, then you can draw another rectangle at the
same location in the background color, but that's about it.

If you have multiple shapes on your canvas and you encapsulate them in
classes and store a collection of objects representing the shapes that
you draw on the canvas, then you can create an Erase method for the
collection that redraws the shape in the background color AND then
causes each of the other shapes that it overlapped to redraw itself.

That's basically what I've done, I just wanted to make sure there
wasn't a more "non-primitive" approach.

Thanks to you both for your responses.

JY
 
That's basically what I've done, I just wanted to make sure there
wasn't a more "non-primitive" approach.

Not sure how you have done this but System.Drawing.Imaging.Metafile could
perhaps help. Also you have the old XOR painting trick
(DrawReversibleFrame). It's hard to say more without knowing what is the
overall goal (undoing the last change, any change, doing a paint program
etc...)
 
Back
Top