1

consider the following paint function (abbreviated):

public void paint(object sender, PaintEventArgs e)
{
    base.OnPaint(e);

    Graphics g = e.Graphics;

    BufferedGraphicsContext context = BufferedGraphicsManager.Current;
    BufferedGraphics buffer = context.Allocate(g, e.ClipRectangle);

    buffer.Graphics.Clear(Color.PaleVioletRed);

    // skip drawing if cond is true (condition is not relevant)
    if(!cond)
    {
        try
        {
          // l is some List<p>
          foreach(Point p in l)
          { 
             // ...calculate X and Y... (not relevant)

             buffer.Graphics.FillEllipse(new SolidBrush(Color.Blue), p.X,p.Y, Point.SIZE,Point.SIZE);
           }                                          
         }

         catch {} // some exception handling (not relevant)

         finally{
            buffer.Render(g);
         }
    }                
    buffer.Render(g);
}

Note that the code above is more or less pseudo-code. I hoped that using the BufferedGraphics-object, the flickering would vanish. In fact, it didn't. At first, I thought that the paint-method would take to long, which it presumably did not (I measured 4-7 ms for each call). If I set cond to true, it still flickers although the paint-method takes almost no time. It might be important that the paint-method will paint on a panel and that I am using a timer to invalidate the panel roughly every 50 ms. How can I finally eliminate the flickering?

Max Beikirch
  • 2,053
  • 5
  • 25
  • 36
  • 2
    Is that finally needed? you are rendering after regardless. – Meirion Hughes Mar 03 '13 at 13:17
  • Thanks, it is really not needed. However, the problem still exists. – Max Beikirch Mar 03 '13 at 14:35
  • 1
    Your code is not eliminating the major reason for flickering, the drawing that's done by OnPaintBackground(). Do favor the built-in support for double-buffering in Winforms. Check [this answer](http://stackoverflow.com/a/3113515/17034) for a double-buffered panel. Or just use PictureBox if you don't need scrolling support, the most basic control that has double-buffering turned on by default. – Hans Passant Mar 03 '13 at 15:08
  • Thanks for hinting on PictureBox! – Max Beikirch Mar 09 '13 at 20:43

1 Answers1

2

Just try setting the property in the constructor:

this.DoubleBuffered = true;

Then you shouldn't need the BufferedGraphics stuff:

public void paint(object sender, PaintEventArgs e)
{
  base.OnPaint(e);

  Graphics g = e.Graphics;       
  g.Clear(Color.PaleVioletRed);

  // skip drawing if cond is true (condition is not relevant)
  if(!cond)
  {
    // l is some List<p>
    foreach(Point p in l)
    { 
      // ...calculate X and Y... (not relevant)  
      g.FillEllipse(new SolidBrush(Color.Blue), p.X,p.Y, Point.SIZE,Point.SIZE);
    }                                          
  }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Thanks for your answer. Unfortunately, the panel does not have such an property. I fear there might be a litte misunderstandment: `paint` is my event listener to onPaint, I didn't derive from Panel. – Max Beikirch Mar 03 '13 at 14:38
  • @MaxBeikirch Try deriving from the panel. Or you can use some reflection: [How to double buffer .NET controls on a form?](http://stackoverflow.com/a/77233/719186) – LarsTech Mar 03 '13 at 14:43
  • Deriving from the panel in the only solution. I hoped that I could avoid that, but sadly it does not work. Thanks for your answer! – Max Beikirch Mar 09 '13 at 20:41