积极答复者
怎么解决图像闪烁问题

问题
-
1.解决图像闪烁问题除了用双缓冲外,还有别的什么方法吗?
我每10毫秒刷新一次图像,用的是BufferedGraphics,感觉画面怎么跟不用双缓冲没什么差别呢~
2.要清除上一次重绘的图形,是不是要调用grafx.Graphics.Clear(Color clr);?
3为什么调用grafx.Render()时,窗体变成了全黑色~
代码:
BufferedGraphicsContext context;
BufferedGraphics grafx;public Form1()
{
InitializeComponent();context = BufferedGraphicsManager.Current;
context.MaximumBuffer = new Size(this.Width + 1, this.Height + 1);
grafx = context.Allocate(this.CreateGraphics(),
new Rectangle(0, 0, this.Width, this.Height));DrawToBuffer(grafx.Graphics);
//Paint += new PaintEventHandler(Form1_Paint);
Timer t = new Timer();
t.Interval = 10;
t.Tick += new EventHandler(t_Tick);
t.Start();
}void t_Tick(object sender, EventArgs e)
{
this.Invalidate();x1 += 1;
y1 += 1;grafx.Graphics.Clear(this.BackColor);
DrawToBuffer(grafx.Graphics);//this.Update();
}private int x1 = 10;
private int y1 = 10;
private int x2 = 10;
private int y2 = 10;private void DrawToBuffer(Graphics g)
{
Pen p = new Pen(Color.Red);
g.DrawEllipse(p, x1, y1, x2, y2);
}protected override void OnPaint(PaintEventArgs e)
{
grafx.Render(e.Graphics);
}
答案
-
我觉得你的重绘是在 WM_PAINT 消息里的。
这样是不正确的。
假如 WM_PAINT 在很多帧以后才绘制。那么你就看不到连续的画面。
而是应该像在 DX 里面那样,在 WM_PAINT 消息族 和 空闲是都重绘。
或者使用专门的控件。
我也有自己的签名档哦!- 已建议为答案 Michael Sun [MSFT]Microsoft employee 2011年12月5日 1:42
- 已标记为答案 Alan_chenModerator 2011年12月12日 2:07
全部回复
-
我觉得你的重绘是在 WM_PAINT 消息里的。
这样是不正确的。
假如 WM_PAINT 在很多帧以后才绘制。那么你就看不到连续的画面。
而是应该像在 DX 里面那样,在 WM_PAINT 消息族 和 空闲是都重绘。
或者使用专门的控件。
我也有自己的签名档哦!- 已建议为答案 Michael Sun [MSFT]Microsoft employee 2011年12月5日 1:42
- 已标记为答案 Alan_chenModerator 2011年12月12日 2:07