C#中利用双缓冲绘图问题
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(); }
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); } }
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(); } }}
[解决办法]
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图像的代码
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();}