title: Own WinForms Control Flickers and Has Bad Performance
body: <p>I have two problems with an own user control which uses bitmaps:</p>
  
  <ol>
  <li>It flickers if it's redrawn via .NET's 'Refresh' method.</li>
  <li>It has a bad performance.</li>
  </ol>
  
  <p>The control consists of three bitmaps:</p>
  
  <ul>
  <li>A static background image.</li>
  <li>A rotating rotor.</li>
  <li>Another image depending on rotor angle.</li>
  </ul>
  
  <p>All used bitmaps have a resolution of 500x500 pixels. The control works like this:
  <a href="https://www.dropbox.com/s/t92gucestwdkx8z/StatorAndRotor.gif">https://www.dropbox.com/s/t92gucestwdkx8z/StatorAndRotor.gif</a> (it's a gif animation)</p>
  
  <p>The user control should draw itself everytime it gets a new rotor angle. Therefore, it has a public property 'RotorAngle' which looks like this:</p>
  
  <pre><code>public double RotorAngle
  {
      get { return mRotorAngle; }
      set
      {
          mRotorAngle = value;
          Refresh();
      }
  }
  </code></pre>
  
  <p><code>Refresh</code> raises the <code>Paint</code> event. The <code>OnPaint</code> event handler looks like this:</p>
  
  <pre><code>private void StatorAndRotor2_Paint(object sender, PaintEventArgs e)
  {
      // Draw the three bitmaps using a rotation matrix to rotate the rotor bitmap.
      Draw((float)mRotorAngle);
  }
  </code></pre>
  
  <p>But when I use this code - which works well in other own user controls - the user control is not drawn at all if control is double buffered via <code>SetStyle(ControlStyles.OptimizedDoubleBuffer, true)</code>. If I don't set this flag to true, the control flickers when being redrawn.</p>
  
  <p>In control constructor I set:</p>
  
  <pre><code>SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  SetStyle(ControlStyles.ContainerControl, false);
  // User control is not drawn if "OptimizedDoubleBuffer" is true.
  // SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  SetStyle(ControlStyles.ResizeRedraw, true);
  SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  </code></pre>
  
  <p>First, I thought it flickers because the background is cleared everytime the control is drawn. Therefore, I set <code>SetStyle(ControlStyles.AllPaintingInWmPaint, true)</code>. But it didn't help.</p>
  
  <p>So, why does it flicker? Other controls work very well with this setup. And why is the controly not drawn if <code>SetStyle(ControlStyles.OptimizedDoubleBuffer, true)</code>.</p>
  
  <p>I found out that the control does not flicker if I invoke my <code>Draw</code> method directly after changing the property <code>RotorAngle</code>:</p>
  
  <pre><code>public float RotorAngle
  {
      get { return mRotorAngle; }
      set
      {
          mRotorAngle = value;
          Draw(mRotorAngle);
      }
  }
  </code></pre>
  
  <p>But this results in a very bad performance, especially in full screen mode. It's not possible to update the control every 20 milliseconds. You can try it yourself. I will attach the complete Visual Studio 2008 solution below.</p>
  
  <p>So, why it is such a bad performance? It's no problem to update other (own) controls every 20 milliseconds. Is it really just due to the bitmaps?</p>
  
  <p>I created a simple visual Visual Studio 2008 solution to demonstrate the two problems:
  <a href="https://www.dropbox.com/s/mckmgysjxm0o9e0/WinFormsControlsTest.zip">https://www.dropbox.com/s/mckmgysjxm0o9e0/WinFormsControlsTest.zip</a> (289,3 KB)</p>
  
  <p>There is an executable in directory <code>bin\Debug</code>.</p>
  
  <p>Thanks for your help.</p>