How to avoid blinking in Form.Invalidate()?

旧街凉风 提交于 2019-12-04 12:08:19

You need to set DoubleBuffered to true.

Since it's a protected property, you'll need to make your own control:

class Canvas : Control {
    public Canvas() { DoubleBufferred = true; }
}

You may need to do all of your drawing to an in memory bitmap first, then paint that bitmap to the form so that it is all drawn on screen at once.

Image buffer = new Bitmap(width, height, colorDepth); //I usually use 32BppARGB as my color depth
Graphics gr = Graphics.fromImage(buffer);

//Do all your drawing with "gr"

gr.Dispose();
e.graphics.drawImage(buffer,0,0);
buffer.Dispose();

You can be more efficient by keeping buffer around longer and not recreating it every frame. but DON'T keep gr around, it should be created and disposed each time you paint.

ad48

People say use DoubleBufferred = true;, but you can easily change the DoubleBufferred parameter on the form to true, without needing to use code.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!