C# 双缓冲有负面影响
我编写了下面的简单程序,每 100 毫秒在屏幕上画一条线(由计时器 1 触发)。我注意到绘图有点闪烁(也就是说,窗口并不总是完全蓝色,而是透出一些灰色)。所以我的想法是使用双缓冲。但当我这样做时,事情变得更糟了。现在屏幕几乎总是灰色的,只有偶尔出现蓝色(由timer2 演示,每 2000 毫秒切换一次 DoubleBuffered
属性)。
对此有何解释?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e) {
Graphics g = CreateGraphics();
Pen pen = new Pen(Color.Blue, 1.0f);
Random rnd = new Random();
for (int i = 0; i < Height; i++)
g.DrawLine(pen, 0, i, Width, i);
}
// every 100 ms
private void timer1_Tick(object sender, EventArgs e) {
Invalidate();
}
// every 2000 ms
private void timer2_Tick(object sender, EventArgs e) {
DoubleBuffered = !DoubleBuffered;
this.Text = DoubleBuffered ? "yes" : "no";
}
}
}
I have written the following simple program, which draws lines on the screen every 100 milliseconds (triggered by timer1). I noticed that the drawing flickers a bit (that is, the window is not always completely blue, but some gray shines through). So my idea was to use double-buffering. But when I did that, it made things even worse. Now the screen was almost always gray, and only occasionally did the blue color come through (demonstrated by timer2, switching the DoubleBuffered
property every 2000 milliseconds).
What could be an explanation for this?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e) {
Graphics g = CreateGraphics();
Pen pen = new Pen(Color.Blue, 1.0f);
Random rnd = new Random();
for (int i = 0; i < Height; i++)
g.DrawLine(pen, 0, i, Width, i);
}
// every 100 ms
private void timer1_Tick(object sender, EventArgs e) {
Invalidate();
}
// every 2000 ms
private void timer2_Tick(object sender, EventArgs e) {
DoubleBuffered = !DoubleBuffered;
this.Text = DoubleBuffered ? "yes" : "no";
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会将所有项目绘制到您自己的缓冲区中,然后立即将其全部复制进去。我在许多应用程序中将其用于图形,并且它对我来说一直非常有效:
编辑:经过进一步调查,看来您的问题在于您将 Graphics 对象设置为:
需要是:
所以你的问题可以通过像我上面那样创建一个手动缓冲区,或者简单地改变你的 Graphics 对象来解决。我已经测试了两者并且它们都有效。
I would just draw all of your items to your own buffer, then copy it all in at once. I've used this for graphics in many applications, and it has always worked very well for me:
EDIT: After further investigation, it looks like your problem is what you're setting your Graphics object to:
needs to be:
So your problem can be solved by either creating a manual buffer like I did above, or simply changing you Graphics object. I've tested both and they both work.
无需使用多个缓冲区或位图对象或任何东西。
为什么不使用Paint事件提供的Graphics对象呢?像这样:
No need to use multiple buffers or Bitmap objects or anything.
Why don't you use the Graphics object provided by the Paint event? Like this:
测试时,尝试在构造函数中将双缓冲属性设置为 true 一次。
您需要利用后台缓冲区。试试这个:
这是一个稍微修改过的版本 双缓冲示例在 MSDN 上。
Try setting the double buffered property to true just once in the constructor while you're testing.
You need to make use of the back buffer. Try this:
It's a slightly hacked around version of a double buffering example on MSDN.