Reputation: 2143
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?
Upvotes: 1
Views: 608
Reputation: 81620
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);
}
}
}
Upvotes: 2