日期:2014-05-18  浏览次数:20908 次

C#中利用双缓冲绘图问题
C# code

 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位图上画图,在试过很多方法无法解决,只好来坛子里问问。
先定义字段
C# code

        private Bitmap MemBmp;
        private Graphics _bufferGraphics;



单独的处理函数如下:
C# code

  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);
            }
        }


显然,以上的代码肯定是由错误的,但是基本可以表达我的想法和意愿,但是不知道怎么解决,希望有人很支支招。在下感激不敬。


------解决方案--------------------
C# code
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();
        }

    }
}

------解决方案--------------------
C# code

        Bitmap b = null;
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //手动
            DrawFirst();
            DrawAnotherSomething();
        }
        private void Dra