C# pictureBox里拖动图片后,然后我在画直线,图片又回到拖动前的地方
我在画直线的代码里,使用了
this.pictureBox1.Invalidata();我知道是这个原因造成的,但这个又不能去掉,
请问怎么办?

解决方案 »

  1.   

    下面是我拖动图片用到的代码:
     private void MyReDrawTest(Point offset)
            {
                Image srcBitmap = this.pictureBox1.Image;
                if (srcBitmap == null)
                    return;            BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current;            BufferedGraphics myBuffer = currentContext.Allocate(this.pictureBox1.CreateGraphics(), this.pictureBox1.DisplayRectangle);            myBuffer.Graphics.Clear(this.pictureBox1.BackColor);            myBuffer.Graphics.DrawImage(srcBitmap, offset);            myBuffer.Render(this.pictureBox1.CreateGraphics());            myBuffer.Dispose();
            }
      

  2.   

    1、将BufferedGraphicsContext 、BufferedGraphics 为全局变量,在构造时赋值
    2、绑定pictureBox重绘事件
    3、在重绘时将图像缓存区图像写入Graphics
    Code        BufferedGraphicsContext currentContext;
            BufferedGraphics myBuffer;
            Graphics g;
            bool userPaint;        public Form1()
            {
                InitializeComponent();
                currentContext = BufferedGraphicsManager.Current;
                g = pictureBox1.CreateGraphics();
                myBuffer = currentContext.Allocate(g, this.pictureBox1.DisplayRectangle);
                userPaint = false;
                pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
            } private void pictureBox1_Paint(object sender, PaintEventArgs e)
            {
                if (userPaint)
                    myBuffer.Render(e.Graphics);
            }        private void MyReDrawTest(Point offset)
            {            Image srcBitmap = this.pictureBox1.Image;
                if (srcBitmap == null)
                    return;            myBuffer.Graphics.Clear(this.pictureBox1.BackColor);            myBuffer.Graphics.DrawImage(srcBitmap, offset);            myBuffer.Render(this.pictureBox1.CreateGraphics());            if (!userPaint)
                    userPaint = true;
            }