我有一个控件可以在位图上绘制一些项目。
我正在使用 Microsoft 手势库来滚动和平移位图。
在平移或滚动时,我的位图经常闪烁。
我根据滚动/平移坐标在 OnPaint
方法中绘制位图的一部分。
示例代码如下:
protected override void OnPaint(PaintEventArgs e)
{
using (Graphics g = e.Graphics)
{
if (!_painted)
{
// drawing items first time
InitilizeBitmap(g);
_painted = true;
}
Rectangle rec = new Rectangle(0, _bitmapLocation.Y, ClientRectangle.Width, ClientRectangle.Height);
using (Graphics gCached = Graphics.FromImage(_cachedBitmap))
{
gCached.Clear(BackColor);
gCached.DrawImage(_bmpControl, 0, 0, rec, GraphicsUnit.Pixel);
}
g.DrawImage(_cachedBitmap, 0, 0);
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
}
有没有办法避免闪烁?
最佳答案
问题是您只使用了 1 个缓冲区,因此绘制操作会立即出现。你需要的是翻页/双缓冲。
您要做的是设置一个新的图形对象,您在其上进行所有绘图,然后在完成后绘制完成的对象。这应该可以消除任何闪烁,因为面向屏幕的位图只更新一次,而且是一次性完成的。
关于c# - 在 .Net Compact Framework 中绘图时控制闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12056678/