WinForm中panel控件放大缩小图片出现闪屏问题,需用缓存问题解决(禁止刷屏)

WinForm 对图像处理本身就不太友好,放大或缩小会刷新界面控件,当然有闪烁。

1、不使用图片作为背景,而用纯色替代,如果是这样的话,也就不会出现闪屏了,但如果UI上有特别要求的,这条路就行不通了。
2、重写Panel,然后在Panel中添加背景图片,而不是把背景图片添加到Form中,重写的Panel代码如下:

View Code 1 /// <summary>
2 /// 一个Panel类,当设置背景图片时,控制其不会闪屏
3 /// </summary>
4 public class BackgroundPanel : Panel
5 {
6 protected override void OnPaintBackground(PaintEventArgs e)
7 {
8 return;
9 }
10
11 protected override void OnPaint(PaintEventArgs e)
12 {
13
14 this.DoubleBuffered = true;
15 if (this.BackgroundImage != null)
16 {
17 e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
18 e.Graphics.DrawImage(this.BackgroundImage, new System.Drawing.Rectangle(0, 0, this.Width, this.Height),
19 0, 0, this.BackgroundImage.Width, this.BackgroundImage.Height,
20 System.Drawing.GraphicsUnit.Pixel);
21 }
22 base.OnPaint(e);
23 }
24 }
3、使用Form的双缓存可以减少闪屏,但效果不明显,可以在Form的Load事件里添加以下代码

View Code1 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
2 this.SetStyle(ControlStyles.DoubleBuffer, true);
3 this.SetStyle(ControlStyles.UserPaint, true);
4 this.SetStyle(ControlStyles.ResizeRedraw, true);
提问者评价
按照你说的,真的成功了,好开心,谢谢你!
posted @ 2014-01-15 14:17  苏苏zhao  阅读(1260)  评论(0编辑  收藏  举报