日期:2014-05-18 浏览次数:21004 次
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 Dra