c# GDI绘图刷新闪烁,求不闪的方法

需求简单,就一个方框从左向右移动,用了Refresh()结果闪的厉害,效率低

窗口属性中双缓冲已设置成True,如果不加 Refresh() 就变成了一条色条

 

void Button1Click(object sender, EventArgs e)
        {
        
           
            
        Graphics huitu;                   
        huitu = this.CreateGraphics(); 

        Pen huabi = new Pen(Color.Blue, 1);
int x, y;
        x = 0;
        y = 0;
        for (int i = 0; i < 1000; i++) {
            Refresh();        
            huitu.DrawRectangle(huabi,x,y,100,50);
            x=x+1;
           
        }

这种处理一般是重画背景颜色,再画就不会有任何闪烁的问题了

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

3幅图像的平滑移动,不闪烁,找3幅图,放到bin文件夹里面,试试看

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace Demo_1
{
    public partial class Form1 : Form
    {
        public class PanelEx : Panel
        {
            public PanelEx()
            {
                SetStyle(
                    ControlStyles.AllPaintingInWmPaint |
                    ControlStyles.OptimizedDoubleBuffer |
                    ControlStyles.ResizeRedraw |
                    ControlStyles.SupportsTransparentBackColor |
                    ControlStyles.UserPaint,
                    true
                    );
                UpdateStyles();
            }
        }
 
        public abstract class DraggableObject
        {
            public abstract Rectangle Region { get; set; }
            public abstract Bitmap Setimage { get; set; }
            public abstract void OnPaint(PaintEventArgs e);
        }
 
        public class Draggable : DraggableObject
        {
            private Rectangle m_Region;
            private Bitmap m_SetImage;
 
            public Draggable(string id, Rectangle regin, Bitmap image)
            {
                m_Region = regin;
                m_SetImage = image;
            }
            public override Rectangle Region { get => m_Region; set => m_Region = value; }
            public override Bitmap Setimage { get => m_SetImage; set => m_SetImage = value; }
            public override void OnPaint(PaintEventArgs e)
            {
                e.Graphics.DrawImage(m_SetImage, m_Region);
            }
        }
        public List<DraggableObject> DesignObjects = new List<DraggableObject>();
 
        public PanelEx panelContent;
 
        public Form1()
        {
            InitializeComponent();
 
            panelContent = new PanelEx()
            {
                Dock = DockStyle.Fill,
                BackgroundImageLayout = ImageLayout.Stretch,
                BackgroundImage = Image.FromFile("back.jpg")
            };
            panelContent.Paint += PanelContent_Paint;
            this.Controls.Add(panelContent);
 
            string[] files = new string[] { "01.png", "02.png", "03.png" };
            for (int i = 0; i < 3; i++)
            {
                Bitmap bmp = Image.FromFile(files[i]) as Bitmap;
                Draggable draggableBlock = new Draggable(DateTime.Now.ToString(), new Rectangle(0, 0, bmp.Width, bmp.Height), bmp);
                DesignObjects.Add(draggableBlock);
            }
            Task.Run(async () =>
            {
                int set_x = 0;
                while (true)
                {
                    DesignObjects[0].Region = new Rectangle(set_x, 100, DesignObjects[0].Region.Width, DesignObjects[0].Region.Height);
                    panelContent.Invalidate();
                    await Task.Delay(5);
                    if (set_x < this.Width) set_x += 1;
                    else set_x = 0;
                }
            });
            Task.Run(async () =>
            {
                int set_y = 0;
                while (true)
                {
                    DesignObjects[1].Region = new Rectangle(100, set_y, DesignObjects[1].Region.Width, DesignObjects[1].Region.Height);
                    panelContent.Invalidate();
                    await Task.Delay(5);
                    if (set_y < this.Height) set_y += 1;
                    else set_y = 0;
                }
            });
            Task.Run(async () =>
            {
                int set_x = 0, set_y = 0;
                while (true)
                {
                    DesignObjects[2].Region = new Rectangle(set_x, set_y, DesignObjects[2].Region.Width, DesignObjects[2].Region.Height);
                    panelContent.Invalidate();
                    await Task.Delay(5);
                    if ((set_y + 1 + DesignObjects[2].Region.Height) < this.Height)
                    {
                        set_x += 1;
                        set_y += 1;
                    }
                    else
                    {
                        set_x = 0;
                        set_y = 0;
                    }
                }
            });
        }
 
        private void PanelContent_Paint(object sender, PaintEventArgs e)
        {
            foreach (DraggableObject item in DesignObjects)
            {
                item.OnPaint(e);
            }
        }
 
    }

 

非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

速戳参与调研>>>https://t.csdnimg.cn/Kf0y