我知道双缓冲是一个经常被谈论的主题,但无论我如何搜索和尝试不同的方法,我仍然无法让控件在没有闪烁的情况下重新绘制自身。这是我的代码:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Emgu.UI
{
public class DoubleBufferedPictureBox : Control
{
const BufferedGraphics NO_MANAGED_BACK_BUFFER = null;
BufferedGraphicsContext GraphicManager;
BufferedGraphics ManagedBackBuffer;
public Bitmap Bitmap { get; set; }
public Rectangle DrawRectangle { get; set; }
public DoubleBufferedPictureBox()
{
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
GraphicManager = BufferedGraphicsManager.Current;
GraphicManager.MaximumBuffer =
new Size(Width + 1, Height + 1);
ManagedBackBuffer =
GraphicManager.Allocate(CreateGraphics(),
ClientRectangle);
Resize += DoubleBufferedPictureBox_Resize;
}
void DoubleBufferedPictureBox_Resize(object sender, EventArgs e)
{
if (ManagedBackBuffer != NO_MANAGED_BACK_BUFFER)
ManagedBackBuffer.Dispose();
GraphicManager.MaximumBuffer =
new Size(Width + 1, Height + 1);
ManagedBackBuffer =
GraphicManager.Allocate(CreateGraphics(),
ClientRectangle);
Refresh();
}
protected override void OnPaint(PaintEventArgs pe)
{
ManagedBackBuffer.Graphics.DrawImage(Bitmap, DrawRectangle);
ManagedBackBuffer.Render(pe.Graphics);
}
}
}
有任何想法吗?