利用双缓冲 提高GDI+绘图的性能

 1using System;  
 2    using System.Collections.Generic;  
 3    using System.ComponentModel;  
 4    using System.Data;  
 5    using System.Drawing;  
 6    using System.Drawing.Drawing2D;  
 7    using System.Text;  
 8    using System.Windows.Forms;  
 9    using System.Drawing.Imaging;  
10    using System.Runtime.InteropServices;  
11    using System.Windows.Media.Imaging;  
12      
13    namespace simplebitmap  
14    {  
15      public partial class Form1 : Form  
16      {  
17      public Form1()  
18      {  
19      InitializeComponent();  
20      }  
21      //画图操作  
22      public void DrawLines(Graphics g)  
23      {  
24      float width = ClientRectangle.Width;  
25      float height = ClientRectangle.Height;  
26      float partX = width / 1000;  
27      float partY = height / 1000;  
28      for (int i = 0; i < 1000; i++)  
29      {  
30      g.DrawLine(Pens.Red, 0, height - (partY * i), partX * i, 0);  
31      g.DrawLine(Pens.Yellow, 0, height - (partY * i), (width) - partX * i, 0);  
32      g.DrawLine(Pens.Blue, 0, partY * i, (width) - partX * i, 0);  
33      }  
34      }  
35          
36      //普通的绘制  
37      private void Simple_Click(object sender, EventArgs e)  
38      {  
39      // 通过‘this’创建Graphics对象  
40      Graphics g = this.CreateGraphics();  
41      g.Clear(this.BackColor);  
42      // 画线  
43      DrawLines(g);  
44      // 释放创建的对象  
45      g.Dispose();  
46      
47      }  
48      
49      //利用双缓冲  
50      private void Bitmap_Click(object sender, EventArgs e)  
51      {  
52      Graphics g = this.CreateGraphics();  
53      g.Clear(this.BackColor);  
54      // 创建一个 Bitmap 对象 ,大小为窗体大小  
55      Bitmap curBitmap = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);  
56      // 创建一个临时 Graphics 对象  
57      Graphics g1 = Graphics.FromImage(curBitmap);  
58      // 通过临时Graphics 对象绘制线条  
59      DrawLines(g1);  
60      // 调用Graphics 对象的DrawImage方法,并画位图  
61      g.DrawImage(curBitmap, 0, 0);  
62      // 释放创建的对象  
63      g1.Dispose();  
64      curBitmap.Dispose();  
65      g.Dispose();  
66      }  
67      }  
68    }