基本图形绘制举例
1.画一个矩形
引用命名空间using System.Drawing.Drawing2D;
protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Rectangle rect = new Rectangle(50, 30, 100, 100); LinearGradientBrush lBrush = new LinearGradientBrush(rect, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal); g.FillRectangle(lBrush, rect); //base.OnPaint(e); }
2.画一个圆弧
protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Pen pn = new Pen(Color.Blue); Rectangle rect = new Rectangle(50, 50, 200, 100); g.DrawArc(pn, rect, 12, 84); }
3.画一条直线
protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Pen pn = new Pen(Color.Blue); Point pt1 = new Point(30, 30); Point pt2 = new Point(110, 100); g.DrawLine(pn, pt1, pt2); }
4.画椭圆
protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Pen pn = new Pen(Color.Blue, 100); Rectangle rect = new Rectangle(50, 50, 200, 100); g.DrawEllipse(pn, rect); }
5.输出文本
protected override void OnPaint(PaintEventArgs e) { Font fnt = new Font("黑体", 26); Graphics g = e.Graphics; g.DrawString("GDI+ World", fnt, new SolidBrush(Color.Red), 14, 10); }
6.填充路径
Graphics g = e.Graphics; g.FillRectangle(new SolidBrush(Color.White), ClientRectangle); GraphicsPath path = new GraphicsPath(new Point[] { new Point(40, 140), new Point(275, 200), new Point(105, 225), new Point(190, 300), new Point(50, 350), new Point(20, 180), }, new byte[] { (byte)PathPointType.Start, (byte)PathPointType.Bezier, (byte)PathPointType.Bezier, (byte)PathPointType.Bezier, (byte)PathPointType.Line, (byte)PathPointType.Line, }); PathGradientBrush pgb = new PathGradientBrush(path); pgb.SurroundColors = new Color[] { Color.Green,Color.Yellow,Color.Red, Color.Blue, Color.Orange, Color.White, }; g.FillPath(pgb, path);