究竟双缓冲区是怎么使用的 才能够防止一会白一会有画的闪烁?
请高手给我个最简单的例子好吗

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Windows.Forms;class Test : Form
    {
    private Bitmap bufferBmp; //缓冲区Bitmap

    public Test()
    {
    bufferBmp = new Bitmap(this.Width,this.Height); //这里忽略了在窗体大小变化时,要相应的改变BufferBmp的大小,你可以自己加上去 ^-^
    this.Paint+= new PaintEventHandler(Test_OnPaint);
    } private void Test_OnPaint(object sender,PaintEventArgs e)
    {
    Graphics g = Graphics.FromImage(bufferBmp); //这个Graphics是来自bufferBmp的,用它画图是画在bufferBmp上的
    //Do sth using g as you wish...
    g.Dispose(); //这时,绘画已完成,释放掉g

    Graphics grfx = e.Graphics; //这个Graphics是来自Screen的,用它画图都直接画到屏幕上
    grfx.DrawImage(0,0,bufferBmp); //将bufferBmp中的内容画到屏幕上
    }
    }这样做的好处就是你可以一次性的将要画的都画出来,然后一起显示给用户。如果不这样而直接画,假如先画二十条直线,再画十个椭圆,那么使用一个不是太快的机器的用户就会看到这些是以一个先后次序画出来的,效果不太好;如果图像是时常更新的,就会有闪烁。还有一点就是,往内存中的缓冲区画是比向显示器画要快的,因而用双缓冲还可以提高速度。
      

  2.   

    你做两个pictureBox,其中的内容相同,交替显示。就是在Clear前画好另外一个PictureBox,然后自己Hide,另外一个Show出来。做一个变量判断哪个该show哪个该hide就可以了。我以前做过一个数据动态采集系统就是这样做的,绝对可行(数据量十分的大)。
      

  3.   

    Tigatron(Illidian) 的方法是好的,在.Net文档中可参考以下内容
    ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfsystemdrawinggraphicsclassfromimagetopic.htm以下为其中的一段例程
    public void FromImageImage(PaintEventArgs e){// Create image.Image imageFile = Image.FromFile("SampImag.jpg");// Create graphics object for alteration.Graphics newGraphics = Graphics.FromImage(imageFile);// Alter image.newGraphics.FillRectangle(new SolidBrush(Color.Black), 100, 50, 100, 100);// Draw image to screen.e.Graphics.DrawImage(imageFile, new PointF(0.0F, 0.0F));// Release graphics object.newGraphics.Dispose();}
    工作过程就是先利用一个Image对象imageFile作为内存的画图底板,画好后用e.Graphics.DrawImage(imageFile, new PointF(0.0F, 0.0F));
    快速画到前台,绝对不会闪烁。
      

  4.   

    太感谢你们了,我做过试验了,你们提供的双缓冲的方法非常可行,
    不过Tigatron好像在最后一句犯了错误grfx.DrawImage(0,0,bufferBmp); 
    应该是grfx.DrawImage(bufferBmp,0,0);
    呵呵,人总会犯错的,没事没事,^_^感谢各位帮我这个菜鸟
      

  5.   

    看看这个
    this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);
    this.SetStyle(ControlStyles.UserPaint,true);
    this.SetStyle(ControlStyles.DoubleBuffer,true);
    不过这样会屏蔽WM_ERASEBKGND消息