具体情况是这样的,鼠标不停的画,闪烁现象不明显,鼠标停在绘图区域不动的画图像就没有了,再动一下鼠标图像就又有了。
private void pictureBox1_Paint_1(object sender, PaintEventArgs e)
{
if (type == DrawType.Line)
{
g0 = e.Graphics;
Bitmap bitmap = new Bitmap(pictureBox1.Width,pictureBox1.Height);//创建临时位图
g3 = Graphics.FromImage(bitmap);//从位图创建绘图面
g3.Clear(Color.White);//清除背景色
g3.SmoothingMode = SmoothingMode.AntiAlias;//设置抗锯齿平滑模式
g0.DrawImage(bitmap, 0, 0);
}}
WM_ERASEBKGND也拦截了
protected override void WndProc(ref Message m)
{
const int WM_ERASEBKGND = 0x14;
if (m.Msg != WM_ERASEBKGND)
base.WndProc(ref m);
}
------解决思路----------------------
对了,在picturebox 上画图双缓冲不会起作用,因为双缓冲是窗体的,所以最方面还是直接在窗体上画。在picturebox上画参照下面代码,我刚写的,无闪烁现象
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
b = new Bitmap(pictureBox1.Width, pictureBox1.Height);
using (Graphics g = Graphics.FromImage(b))
{
g.FillRectangle(Brushes.White, pictureBox1.ClientRectangle);
}
pictureBox1.Image = (Image)b.Clone();
}
Bitmap b;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
g.DrawImage(b, 0, 0);
g.DrawLine(Pens.Red, new Point(0, 0), e.Location);
}
pictureBox1.Refresh();
}
}