Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello

how to remove the rectangle graphics from picture box, I am able to draw a rectangle graphics using this code:

Dim g As System.Drawing.Graphics
Dim cyan As New SolidBrush(Color.Yellow)
Dim rect As New Rectangle(x1, y1, 15, 15)
g.FillRectangle(cyan, rect)

Is it possible to remove this rectangle graphics i had draw, Please tell me how to do so.

Thanks.
Posted

You simply call Invalidate on the PictureBox control and NOT draw your box on it.

This will force the control to redraw itself without the box you drew.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Dec-15 14:16pm    
This time, my 4. It would be better to say that PictureBox should not be used at all. It does not make any sense. Please see Solution 2.
—SA
MinhajAli 22-Dec-15 10:14am    
Thanks for reply.
But Picturebox.Invalidate is not forcing to redraw in my case.
actually i am loading an image in picturebox and on that image i am drawing the rectangle on MouseDown event.
Dave Kreskowiak 22-Dec-15 11:54am    
From the code you posted, it's impossible to be sure what you're actually drawing on. You never show what g is set to, other than saying it's going to hold a Graphics object.

If you are drawing directly on the Bitmap image itself, you cannot remove the pixels once drawn without having the original copy of the Bitmap somewhere. Once you draw on the Bitmap object, that's it, you cannot get the original pixels back unless you reload the Bitmap from it's original source data.

If you're drawing on the surface of the PictureBox, you simply Invalidate it and do NOT draw your rectangle on the control in the Paint event of the PictureBox. It's that simple.
With your code, you should not use PictureBox at all — it does nothing useful for you, nothing at all, only adds some pain in the… let's say, in the neck and steals some of your development time. What to do instead? Something a lot more simple. Please see my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
How to speed up my vb.net application?[^].

And yes, you need to have some data model which may include, say, a Boolean flag named "clear". If it's "clear", just don't do any rendering. When you call Control.Invalidate(), your graphics will be refreshed, and your rendering method will be called not drawing anything.

—SA
 
Share this answer
 
Comments
MinhajAli 22-Dec-15 11:39am    
Thanks for your reply.

i follow your given link but no luck.

In my case i am loading an image on pucturebox and counting the mouse click on that image for finding specific thing on image.Suppose i am loading the image of Hair and the user want to count the hair. for user information i am drawing rectangle on it that which hair user had already count.
if user mistakenly click on wrong place than there is a button to undo so how to remove that particular rectangle.

is there any other solution..?
Sergey Alexandrovich Kryukov 22-Dec-15 11:45am    
This is not a "reply"; this is a comprehensive answer. I have no idea what you are missing, but you can ask follow-up questions. "Click on wrong place" is unrelated to your original question. Implement undo. "How to remove" question suggests that you did not get the simple idea of how rendering is done.
Other solution? Why? It simply makes no sense. All rendering is done in the same way. It's based on Windows API (GDI+), the whole library.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900