C#中使用双缓冲来避免绘制图像过程中闪烁

时间:2023-03-08 23:26:39
C#中使用双缓冲来避免绘制图像过程中闪烁

自己所做项目中,在显示医学图像的界面中,当鼠标拖动图像时,不断刷新从后台获取新的图像,而整个过程就很诡异,一直闪个不停。

找到的一个可行方法是:在用户控件的构造函数中加入以下代码:

SetStyle(
ControlStyles.OptimizedDoubleBuffer
| ControlStyles.ResizeRedraw
| ControlStyles.Selectable
| ControlStyles.AllPaintingInWmPaint
| ControlStyles.UserPaint
| ControlStyles.SupportsTransparentBackColor,
true);

        this.SetStyle(ControlStyles.UserPaint, true);//自绘
this.SetStyle(ControlStyles.DoubleBuffer, true);// 双缓冲
this.SetStyle(ControlStyles.ResizeRedraw, true);//调整大小时重绘
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);// 双缓冲
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); //透明效果

以此方法来开启双缓冲。