title: Problems re-drawing an image on a double-buffered control without flickering
body: <p>I know that double-buffering is an often-talked subject but no matter how much I searched and tried different approaches, I still can't get the control to re-draw itself without a flicker. Here's my code:</p>
  
  <pre><code>using System;
  using System.Drawing;
  using System.Windows.Forms;
  
  namespace Emgu.UI
  {
      public class DoubleBufferedPictureBox : Control
      {
          const BufferedGraphics NO_MANAGED_BACK_BUFFER = null;
  
          BufferedGraphicsContext GraphicManager;
          BufferedGraphics ManagedBackBuffer;
  
          public Bitmap Bitmap { get; set; }
          public Rectangle DrawRectangle { get; set; }
  
          public DoubleBufferedPictureBox()
          {
              SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  
              GraphicManager = BufferedGraphicsManager.Current;
              GraphicManager.MaximumBuffer =
                     new Size(Width + 1, Height + 1);
              ManagedBackBuffer =
                  GraphicManager.Allocate(CreateGraphics(),
                                                 ClientRectangle);
  
              Resize += DoubleBufferedPictureBox_Resize;
          }
  
          void DoubleBufferedPictureBox_Resize(object sender, EventArgs e)
          {
              if (ManagedBackBuffer != NO_MANAGED_BACK_BUFFER)
                  ManagedBackBuffer.Dispose();
  
              GraphicManager.MaximumBuffer =
                    new Size(Width + 1, Height + 1);
  
              ManagedBackBuffer =
                  GraphicManager.Allocate(CreateGraphics(),
                                                  ClientRectangle);
              Refresh();
          }
  
          protected override void OnPaint(PaintEventArgs pe)
          {
              ManagedBackBuffer.Graphics.DrawImage(Bitmap, DrawRectangle);
              ManagedBackBuffer.Render(pe.Graphics);
          }
  
      }
  }
  </code></pre>
  
  <p>Any ideas?</p>