如何修复WinForms表单中的闪烁?

我不断画帧,我需要表格不要闪烁。 我该如何做到这一点?

public partial class Form1 : Form { Image[] dude = new Image[3]; static int renderpoint = 0; int lastimage = 0; public Form1() { dude[1] = new Bitmap(@"snipe1.bmp"); dude[0] = new Bitmap(@"snipe0.bmp"); InitializeComponent(); } private void Form1_Shown(object sender, EventArgs e) { MainLoop(); } private void MainLoop() { double FPS = 10; long ticks1 = 0; long ticks2 = 0; double interval = (double)Stopwatch.Frequency / FPS; while (true) { ticks2 = Stopwatch.GetTimestamp(); if (ticks2 >= ticks1 + interval) { ticks1 = Stopwatch.GetTimestamp(); MoveGraphics(); this.Refresh(); } Thread.Sleep(1); } } private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Rectangle rect = new Rectangle(renderpoint, 0, 100, 100); Color lowcolor = Color.FromArgb(0, 128, 64); Color highcolor = Color.FromArgb(0, 128, 64); ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetColorKey(lowcolor, highcolor); if (lastimage == 1) { lastimage = 0; g.DrawImage(dude[1], rect, 0, 0, 100, 100, GraphicsUnit.Pixel, imageAttr); } else { lastimage = 1; g.DrawImage(dude[0], rect, 0, 0, 100, 100, GraphicsUnit.Pixel, imageAttr); } } void MoveGraphics() { if (renderpoint > 950) { renderpoint = 0; } else { renderpoint += 10; } } } 

有当前的代码。 建议?

将其粘贴到Form1构造函数中:

 this.DoubleBuffered = true; 
  1. Paint事件处理程序中进行渲染
  2. 禁用自动删除背景。
  3. 通过样式或手动启用双缓冲。
  4. 如果要重新绘制,请调用Invalidate

如果您正在尝试使用平滑动画,那么我是否可以建议您跳转到WPF,OpenGL或XNA。 GDI +不是为动画设计的(Windows消息循环不是实时系统,所以你总是会有抖动)。