Glad rap
Glad rap

Reputation: 33

C# Remove drawing from PictureBox

I have a PictureBox pic , and an Image img, pic.Image = img
and I draw some rectangles on the Image using

Graphics g = Grpahics.FromImage(pic.Image);
g.DrawRectangle(...);

But at one point, I want to remove the rectangle from the Image,I tried

pic.Image = getOriginalImage();
pic.Refresh();

but the image remains the same(Rectangles are still on top of the Image) I know there's a method graphics.Clear(Color),but it replace the entire Image with a solid color
How do I remove the drawing only from the PictureBox?
Thanks

Edit: I already saved the original and when I erase I use the original image

Upvotes: 3

Views: 5830

Answers (2)

albattran
albattran

Reputation: 1907

You are overwriting the original image. Save the image locally and clone it into the PictureBox.Image property. Then clone again when you want to erase

Upvotes: 4

akton
akton

Reputation: 14386

Draw the background image again using Graphics.DrawImage() or assign the image again to the Image property. Using a Graphics object in this way is writing directly to the image displayed in the PictureBox and it does not keep separate copies of what is written using a Graphics object and the background Image.

Upvotes: 2

Related Questions