Graphics question

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
 
F

Fred Mellender

You could draw it again with the background color, assuming you have not
drawn over it in the mean time with some other geometry.
 
H

Harlan Messinger

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.
 
J

Jon

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
 
P

Patrice

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...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top