首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2012-09-05 
C#中利用双缓冲绘图问题C# code protected override void OnPaint(PaintEventArgs e){base.OnPaint(e)//

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


[解决办法]
paint 方法最好不要放绘制MenBmp图像的代码

C# code
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();} 

热点排行