-1

I know there are many questions on this, but I just can't get it to work. I have this event:

private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{ 
    e.Graphics.DrawRectangle(new Pen(selectionBrush, 1), selectionRectangle);
}

It works as it should. Draws a colored rectangle over the image I put inside picture box. But after I draw that rectangle, I want to erase it. I basically use it as selection area for cropping the image. Is there a way that I can erase the rectangle that I have drawn? Thanks.

Here is where I want to erase the rectangle:

private void button1_Click(object sender, EventArgs e)
    {
        //here are some examples of what I have tried
        pictureBox1.Invalidate();
        Invalidate();
    }
alan
  • 3
  • 4
  • 2
    Set a flag to draw it or not? – Ňɏssa Pøngjǣrdenlarp Dec 05 '18 at 18:05
  • `this.Invalidate()` ? `Graphics.Clear` ? Are you wanting to 'retain' the shape though? To clarify my question - you're wanting to draw the rectangle, **immediately clear it out** right after? What are you trying to accomplish? – gravity Dec 05 '18 at 18:06
  • It would help if we understood better what the overall goal here is, what you're trying to do, and why this is part of the "solution". – Damien_The_Unbeliever Dec 05 '18 at 18:08
  • If you're trying to perform some kind of interactive screen drawing consult [How To Draw Shapes In WinForms](https://stackoverflow.com/questions/49991039/). – Dour High Arch Dec 05 '18 at 18:13
  • @gravitymixes as I said in the question, I need to crop an image that is set in picture box, so I need to mark an area for cropping, and I want that rectangle to disappear as soon as I crop the image. For instance, I will call a method on button click to crop an image, and I want in that method to erase the rectangle that I have drawn. I also accept other suggestions. – alan Dec 05 '18 at 18:18
  • 1
    Possible duplicate of [how to clear the graphics(rectangle shape) in picturebox](https://stackoverflow.com/questions/10442279/how-to-clear-the-graphicsrectangle-shape-in-picturebox) – Alessio Raddi Dec 05 '18 at 18:19
  • @AlessioRaddi I have tried these. They do not work for some reason. – alan Dec 05 '18 at 18:20
  • @trckojr "for some reason" - what **does** happen? It's still not clear as to what the **problem** is you're encountering. Do you get an exception or error of any kind? You've shown the effort at drawing, but have not shown what effort you've put into in regards to the other suggestions. – gravity Dec 05 '18 at 18:21
  • @trckojr Do you get any error? Or the rectangle just doesn't "disappear"? – Alessio Raddi Dec 05 '18 at 18:23
  • If the picturebox doesn't get refresh it may still look like the rectangle exists when it actually was deleted. – jdweng Dec 05 '18 at 18:24
  • No error. The rectangle just does not disappear. – alan Dec 05 '18 at 18:25
  • Also, tried the refresh(). – alan Dec 05 '18 at 18:25
  • 1
    Your paint routine has a call to `DrawRectangle`. Then you invalidate the rectangle. At that point, your paint routine will get called again, and, if your code to draw the rectangle is still there, it will get redrawn. You need to make it so the `DrawRectangle` call no longer gets called once you don't need it any more (with an `if` statement or something). You also probably want to inflate the size of the invalidation rectangle by one pixel all around to make sure you don't leave any bread crumbs behind – Flydog57 Dec 05 '18 at 18:29
  • @Flydog57 Thank you so much. That was the problem. Invalidate() does work, but It would just be repainted again. – alan Dec 05 '18 at 18:36

1 Answers1

0

Your Paint routine has a call to DrawRectangle. Then you invalidate the rectangle. At that point, your paint routine will get called again, and, if your code to draw the rectangle is still there, it will get redrawn. You need to make it so the DrawRectangle call no longer gets called once you don't need it any more (with an if statement or something).

You also probably want to inflate the size of the invalidation rectangle by one pixel all around to make sure you don't leave any bread crumbs behind

Flydog57
  • 6,851
  • 2
  • 17
  • 18