Draw rectangle over a picturebox problem

Hi all. I try to draw a rectangle over a picturebox, but I face a problem. The object (rectangle) is drawing successfully but the problem is that when I move some other picturebox which is over the first picturebox several times and it passed over the location where I have drawn the object the rectangle disappears. I use picBox.CreateGraphics().DrawRectangle(...) to draw the rectangle over the pictureBox. How can I draw something over a pictureBox so it will not disappear after some other pictureBox passed over it? Thanks a lot.
mg_miro_86Asked:
Who is Participating?
 
CuteBugConnect With a Mentor Commented:
Instead of getting the Graphics of the pictureBox, try to get the graphics of the image displayed in the picturebox.
Then handle it during the Paint event.

This link will provide more detail

http://www.bobpowell.net/pictureboxhowto.htm
http://www.bobpowell.net/creategraphics.htm
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Do NOT use CreateGraphics().

Instead, use the graphics supplied in the PAINT Event of the PictureBox.'

It is passed via the "e" Parameter:
public void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    e.DrawRectangle(...);
}

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Just noticed I have a small typo in my snippet.

Obviously, this:

    e.DrawRectangle(...);

Should be:

    g.DrawRectangle(...);

If you use the Paint() Event graphics then you can draw "on top" any Image currently being displayed in the PictureBox...WITHOUT permanently changing the actual image.  Whether this is desirable or not really depends on your application...  =)
0
 
mg_miro_86Author Commented:
Thank you CuteBug:. You solution solved my problem. Thanks and for Idle_Mind for his reply. Have a nice day. ;)
0
All Courses

From novice to tech pro — start learning today.