How can I stop a PictureBox Image from becoming distorted when another window is moved over it?
Posted on 2011-03-09
I have written a .NET 2.0 C# program (Microsoft Visual Studio 2008) that has a PictureBox (named PB_1) on a form. I have entered code into the Paint event for the PictureBox (i.e., into PB_1_Paint(...)) that draws an image into the PictureBox by using the Graphics property of the PaintEventArgs object that is passed to the Paint event handler (i.e., e.Graphics) by using e.Graphics.DrawRectangle(...). The rectangle is drawn without any problem. However, if I move another window over the PictureBox then the drawing in the PictureBox becomes distorted.
As a temporary fix to this I added code to the ResizeEnd event handler for the form that does nothing but invalidate the PictureBox by calling PB_1.Invalidate(). Thus, when the drawing in the PictureBox becomes distorted I simply grab the lower-right corner of the form and adjust the size of the form slightly which causes the drawing in the PictureBox to be redrawn.
How can I fix this? That is, how can I make sure that the drawing in the PictureBox does not become distorted when another form is moved over my form?