1、绘制一条线
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; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // 创建GDI,在指定的panel上绘制 Graphics g = panel1.CreateGraphics(); //创建两个点 Point n1 = new Point(20, 20); Point n2 = new Point(100, 100); //创建画笔 Pen p = new Pen(Brushes.Red,1); g.DrawLine(p, n1, n2); } } }
2、绘制矩形
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; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // 创建GDI,在指定的panel上绘制 Graphics g = panel1.CreateGraphics(); Pen p = new Pen(Brushes.Red); g.DrawRectangle(p, 50, 50, 60, 60); } } }
3、绘制扇形
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; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // 创建GDI,在指定的panel上绘制 Graphics g = panel1.CreateGraphics(); Pen p = new Pen(Brushes.Red); Rectangle re = new Rectangle(50, 50, 60, 60); g.DrawPie(p,re, 0, 60); } } }
4、绘制三角形
三角形类 Tiangle
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; namespace WindowsFormsApplication1 { public class Triangle { PointF A, B, C; public Triangle(PointF a, PointF b, PointF c) { A = a; B = b; C = c; } /// <summary> /// 绘制三角形 /// </summary> /// <param name="g"></param> public void Draw(Graphics g) { Pen pen = new Pen(Color.Red, 1); g.DrawLine(pen, A, B); g.DrawLine(pen, B, C); g.DrawLine(pen, C, A); } /// <summary> /// 三角形旋转 /// </summary> /// <param name="degrees"></param> public void Rotate(int degrees) { float angle = (float)(degrees / 360f * Math.PI); float newX = (float)(A.X * Math.Cos(angle) - A.Y * Math.Sin(angle)); float newY = (float)(A.X * Math.Sin(angle) + A.Y * Math.Cos(angle)); A.X = newX; A.Y = newY; newX = (float)(B.X * Math.Cos(angle) - B.Y * Math.Sin(angle)); newY = (float)(B.X * Math.Sin(angle) + B.Y * Math.Cos(angle)); B.X = newX; B.Y = newY; newX = (float)(C.X * Math.Cos(angle) - C.Y * Math.Sin(angle)); newY = (float)(C.X * Math.Sin(angle) + C.Y * Math.Cos(angle)); C.X = newX; C.Y = newY; } } }
form1
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; namespace WindowsFormsApplication1 { public partial class Form1 : Form { Triangle t; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { PointF A = new PointF(0, -10); PointF B = new PointF(10, 10); PointF C = new PointF(-10, 10); t = new Triangle(A, B, C); Graphics g = panel1.CreateGraphics(); g.TranslateTransform(50, 50); t.Draw(g); } } }