using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace GDIPlus { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void OnPaint(object sender, PaintEventArgs e) { //Graphics g = e.Graphics; //Random r = new Random(); //g.FillRectangle(Brushes.White, ClientRectangle); //for (int i = 0; i < ClientRectangle.Width; i+=10 ) //{ // for (int j = 0; j < ClientRectangle.Height; j+=10 ) // { // Color c = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255)); // using(Pen p = new Pen(c, 1)) // { // g.DrawLine(p, new Point(0, 0), new Point(i, j)); // } // } //} //使用双缓冲 Graphics displayGraphics = e.Graphics; Random r = new Random(); //新建一个图像,从图像中获取DC的Graphics,以便将线条画在图像上 Image image = new Bitmap(ClientRectangle.Width, ClientRectangle.Height); Graphics g = Graphics.FromImage(image); g.FillRectangle(Brushes.White, ClientRectangle); for (int i = 0; i < ClientRectangle.Width; ++i ) { for (int j = 0; j < ClientRectangle.Height; j+=10 ) { Color c = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255)); Pen p = new Pen(c, 1); g.DrawLine(p, new Point(0, 0), new Point(i, j)); p.Dispose(); } } displayGraphics.DrawImage(image, ClientRectangle); image.Dispose(); } } }