标签:
public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Graphics g = this.CreateGraphics();//创建GDI对像 //创建画笔(颜色) Pen npen = new Pen(Brushes.Red); //创建两个点 Point n1 = new Point(20, 20); Point n2 = new Point(255, 255); g.DrawLine(npen, n1, n2); } private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = this.CreateGraphics();//创建GDI对像 //创建画笔(颜色) Pen npen = new Pen(Brushes.Red); //创建两个点 Point n1 = new Point(20, 20); Point n2 = new Point(255, 255); g.DrawLine(npen, n1, n2); } private void button2_Click(object sender, EventArgs e) { Graphics ng = this.CreateGraphics(); //创建画笔 Pen npen = new Pen(Brushes.Red); Size sz=new System.Drawing.Size(100,100); Rectangle res = new Rectangle(new Point(50, 50), sz); ng.DrawRectangle(npen, res); } private void button3_Click(object sender, EventArgs e) { Graphics g = this.CreateGraphics(); Pen npen = new Pen(Brushes.Blue); Size sz = new System.Drawing.Size(180, 180); Rectangle rec = new Rectangle(new Point(150, 150), sz); g.DrawPie(npen, rec, -50, -50); } private void pictureBox1_DoubleClick(object sender, EventArgs e) { Random rd = new Random(); string str = null; for (int i = 0; i < 5; i++) { int n1=rd.Next(1, 10); str += n1; } Bitmap bmp=new Bitmap(120,40); //创建gdi Graphics gd = Graphics.FromImage(bmp); //画图 for (int i = 0; i < str.Length; i++) { Point pt = new Point(i * 20, 0); string[] fonts={"微软雅黑","宋体","黑体","隶书","仿宋"}; Color[] colors={Color.Red,Color.Black,Color.Blue,Color.Orange,Color.Plum}; gd.DrawString(str[i].ToString(), new Font(fonts[rd.Next(0, 5)], 15, FontStyle.Bold), new SolidBrush(colors[rd.Next(0, 5)]), pt); } //给图片加线 for (int i = 0; i < 20; i++) { Point xpt1 = new Point(rd.Next(0, bmp.Width), rd.Next(0, bmp.Height)); Point xpt2 = new Point(rd.Next(0, bmp.Width), rd.Next(0, bmp.Height)); gd.DrawLine(new Pen(Brushes.Red), xpt1, xpt2); } //给图片加点 for (int i = 0; i < 50; i++) { Point p1 = new Point(rd.Next(0, bmp.Width), rd.Next(0, bmp.Height)); bmp.SetPixel(p1.X, p1.Y, Color.Black); } //将图片放到pictureBox中 pictureBox1.Image = bmp; }
标签:
原文地址:http://www.cnblogs.com/zywf/p/4544877.html