protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
 
            //手动
            Bitmap MemBmp = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
            _bufferGraphics = Graphics.FromImage(MemBmp);
            _bufferGraphics.Clear(this.BackColor);            //Draw something in the MemBmp
            _bufferGraphics.DrawImage(_backImg, 0, 0);
            _bufferGraphics.DrawRectangle(Pens.Red,100.100,300,200);
            //图像呈现
            Graphics g = e.Graphics;
            g.DrawImage(MemBmp, 0, 0);            //Dispose
            MemBmp.Dispose();
            _bufferGraphics.Dispose();
        }
以上是我定义的双缓冲处理过程,基本没有什么问题,但是我现在想单独添加个函数,这个函数的功能是在原来的MemBmp位图上画图,在试过很多方法无法解决,只好来坛子里问问。
先定义字段        private Bitmap MemBmp;
        private Graphics _bufferGraphics;
单独的处理函数如下:  private void DrawAnotherSomething()
        {
           //Draw another things that i want in the MemBmp bitmap
            _bufferGraphics.DrawRectangle(Pens.Black,0,0,50,50);           //图像呈现  问题就出现在这,不知道该如何去呈现图像。
            using(Graphics g = this.CreatGraphics()
            {
               g.DrawImage(MemBmp, 0, 0);
            }
        }显然,以上的代码肯定是由错误的,但是基本可以表达我的想法和意愿,但是不知道怎么解决,希望有人很支支招。在下感激不敬。

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication4
    {
        public partial class Form1 : Form
        {
            private Bitmap MemBmp;
            private Graphics _bufferGraphics;
            public Form1()
            {
                InitializeComponent();
            }
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
     
                //手动
                 MemBmp = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
                _bufferGraphics = Graphics.FromImage(MemBmp);
                _bufferGraphics.Clear(this.BackColor);            //Draw something in the MemBmp
               // _bufferGraphics.DrawImage(_backImg, 0, 0);
                _bufferGraphics.DrawRectangle(new Pen(Color.Red,3),new Rectangle(10,10,300,300));
                //图像呈现
                Graphics g = e.Graphics;
                g.DrawImage(MemBmp, 0, 0);            //Dispose
             //   MemBmp.Dispose();
                _bufferGraphics.Dispose();
            }
             private void DrawAnotherSomething()
            {
               //Draw another things that i want in the MemBmp bitmap
                //_bufferGraphics.DrawRectangle(Pens.Black,0,0,50,50);           //图像呈现  问题就出现在这,不知道该如何去呈现图像。
                using(Graphics g =Graphics.FromImage(MemBmp))
                {
                    g.DrawLine(new Pen(Color.Red,3), 10, 10, 20, 20);
                    g.DrawRectangle(new Pen(Color.Red, 3), new Rectangle(10, 10, 30, 30));
                }
                Graphics gg = this.CreateGraphics();
                gg.DrawImage(MemBmp, 0, 0);
            }        private void button1_Click(object sender, EventArgs e)
            {
                DrawAnotherSomething();
            }    }
    }
      

  2.   


            Bitmap b = null;
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                //手动
                DrawFirst();
                DrawAnotherSomething();
            }
            private void DrawFirst()
            {
                 b = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
                Graphics _bufferGraphics = Graphics.FromImage(b);
                //_bufferGraphics.Clear(this.BackColor);            //Draw something in the MemBmp
                _bufferGraphics.DrawImage(Image.FromFile(@"1.jpg"), 0, 0);            //图像呈现
                Graphics g = this.CreateGraphics();
                g.DrawImage(b, 0, 0);            //Dispose
                b.Dispose();
                _bufferGraphics.Dispose();
            }
            private void DrawAnotherSomething()
            {
                b = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
                Graphics _bufferGraphics = Graphics.FromImage(b);            _bufferGraphics.DrawRectangle(Pens.Green,100,100,50,50);            //图像呈现
                Graphics g = this.CreateGraphics();
                g.DrawImage(b, 0, 0);            //Dispose
                b.Dispose();
                _bufferGraphics.Dispose();
            }
      

  3.   


    对不起 这么晚才回复你,谢谢你的回答。你的代码我测试了,跟我原来的方法一样,即如果把 MemBmp 提升为字段,在初始化的时候会自动的把其初始化为null,那么在 DrawAnotherSomething 此函数中调用null对象是会出现错误的。
      

  4.   

    我无语了    看看你的代码   //Dispose
                MemBmp.Dispose();
                _bufferGraphics.Dispose();   我的代码里面这个被我注释了   还有就是当你打开窗体时候就会执行Onpaint方法对窗体进行重绘  MemBmp怎么可能为NULL?  
      

  5.   

    MemBmp.Dispose();  你加这句肯定是不行的  在 DrawAnotherSomething方法里面调用MemBmp肯定为NULL   C#里面这个DrawAnotherSomething()叫方法  不要说函数  那是C里面的
      

  6.   

    if null{}else{
      '''''''''''''
    }
      

  7.   

    你的问题是如何找Form的Graphics,使用Graphics.FromHwnd方法,参数使用Form的Handle属性就可以了。如下是MSDN中的例子,可以参考:    // Get handle to form.
        IntPtr hwnd = this.Handle;    // Create new graphics object using handle to window.
        Graphics newGraphics = Graphics.FromHwnd(hwnd);    // Draw rectangle to screen.
        newGraphics.DrawRectangle(new Pen(Color.Red, 3), 0, 0, 200, 100);    // Dispose of new graphics.
        newGraphics.Dispose();
      

  8.   

    paint 方法最好不要放绘制MenBmp图像的代码protected override void OnPaint(PaintEventArgs e)
    {
     base.OnPaint(e);
     if(MemBmp!= null) 
     e.Graphics.DrawImage(MemBmp, 0, 0);
    }
    //把绘制MenBmp的方法单独抽象出来
    void getBitmap()
    {
        MemBmp = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
        _bufferGraphics = Graphics.FromImage(MemBmp);
        _bufferGraphics.Clear(this.BackColor);    //Draw something in the MemBmp
        _bufferGraphics.DrawImage(_backImg, 0, 0);
        _bufferGraphics.DrawRectangle(Pens.Red,100.100,300,200);   
        _bufferGraphics.Dispose();
    }