-1

I have a Winform application with a picturebox that I want to be able to draw on. To do that, I generated a method for the picturebox.Paint event. However, ever since I added a paint event to my picturebox my winform does not display properly on start. Starting the application only partially loads the form

enter image description here

And I have to drag the application window with my mouse to get it to finish rendering

enter image description here

Not only that, for anything I do that changes anything on the form, the change is not visible until I drag the window. I know something about the paint event is triggering it because getting rid of it fixes the problem. Having an empty paint event method also stops the bug. Even a paint method as simple as this

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    pictureBox1.Image = null;
}

causes the bug to occur. Any help would be greatly appreciated!

Nick Gilbert
  • 4,159
  • 8
  • 43
  • 90
  • 2
    It's possible that by setting the pictureBox to null, it causes the paint event to fire again which set the pictureBox to null which causes the paint event to fire again etc.... You should never perform any operation in a paint event that could cause the paint event to fire again otherwise you might get infinite recursion. – Chris Dunaway Apr 14 '16 at 15:07
  • This is clearly __not a valid__ content for a Paint method! Yes, it is simple but so is an Invalidate() in the Paint event.. It is strictly for graphics you draw with the e.Graphics object. – TaW Apr 14 '16 at 15:18
  • I changed my code to avoid any chance of infinite recursion which seems to have fixed my problem. I still have one separate issue which I've opened a question for http://stackoverflow.com/questions/36628305/scaling-picturebox-does-not-change-image-at-all – Nick Gilbert Apr 14 '16 at 16:03

1 Answers1

0

I changed my paint event method to avoid changing any properties of the picturebox. I also added a boolean flag so that it only draws when I want it to instead of every time the form needs redrawing.

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (isDraw)
            {
                 //Do Stuff
            }
        }
Nick Gilbert
  • 4,159
  • 8
  • 43
  • 90