title: C# : Remove Paint Event from PictureBox
body: <p>Sorry for my bad English.</p>
  
  <p>I have a picturebox where I draw 100000 shapes (but there may be more).
  The drawing is made in the Paint Handler of the picturebox.</p>
  
  <p>The problem is : When I Resize the form (where the picturebox is), use the scrollbar of the panel which contain it, come from another application, ... the paint handler is called...
  But the paint process takes quite some time and the user must wait until the paint have finished...</p>
  
  <p>I tried the folowing :</p>
  
  <ul>
  <li>Create a bitmap where i draw the shapes</li>
  <li>In the paint handler, i copy the bitmap in the picturebox</li>
  </ul>
  
  <p>NB : The size and the content of the picturebox can change, so the bitmap must also change.
  The creation of the bitmap + restoration of the bitmap make the application slower than before :</p>
  
  <pre><code>Bitmap bmp = new Bitmap(picturebox.Width, picturebox.Height);
  // draw in Graphics.FromImage(bmp);
  picturebox.Invalidate();
  bmp.Dispose();
  </code></pre>
  
  <p>I also tried with a boolean flag : canRedraw.
  I set it true when the content of the picturebox change and then I call picturebox.Invalidate(). In the paint handler, I check if (canRedraw) and if so, I redraw the content (and canRedraw = false), else I make nothing.
  But with this last solution, when I make something with the form, my picturebox is cleared...</p>
  
  <p>Do you have any idea of how I can make this :</p>
  
  <blockquote>
    <p>If you are a method that change the content of the picturebox then you can redraw the picturebox, else you leave the visual content of the picturebox <strong>unchanged</strong>.</p>
  </blockquote>
  
  <p>Can you help me ?</p>
  
  <p>Thank you very much :)</p>