一、DrawBezier 画立体的贝尔塞曲线
private void frmGraphics_Paint(object sender, PaintEventArgs e) { Graphics g = this.CreateGraphics();// e.Graphics; Pen blackPen = new Pen(Color.Red, 3); //从第一个点到第四个点绘制贝塞尔曲线。 第二个和第三个点是确定曲线的形状的控制点。 Point start = new Point(100, 100); Point control1 = new Point(200, 10); Point control2 = new Point(350, 50); Point end = new Point(500, 100); //画立体的贝尔塞曲线. //DrawBezier有多种重载这里就不一一说明了 g.DrawBezier(blackPen, start, control1, control2, end); }
效果图:
二、DrawArc 画弧
private void frmGraphics_Paint(object sender, PaintEventArgs e) { Graphics g = this.CreateGraphics();// e.Graphics; Pen p = new Pen(Color.Black, 3); // 定义矩形,用于确定弧线的边界,顾名思义就是在一个定义好的矩形中画画线 // 现在创建的是一个正方形(正方形画圆,长方形画椭圆) Rectangle rect = new Rectangle(10, 10, 100, 100); // 从 x 轴到弧线的起始点沿顺时针方向度量的角(以度为单位) float startAngle = 45.0F; // 从 startAngle 参数到弧线的结束点沿顺时针方向度量的角(以度为单位) float sweepAngle = 180.0F; // 画矩形 g.DrawRectangle(p, rect); // 画弧. g.DrawArc(p, rect, startAngle, sweepAngle); p = new Pen(Color.Red, 3); // 定义矩形,用于确定弧线的边界,顾名思义就是在一个定义好的矩形中画画线 // 现在创建的是一个长方形(正方形画圆,长方形画椭圆) rect = new Rectangle(200, 200, 200, 100); // 从 x 轴到弧线的起始点沿顺时针方向度量的角(以度为单位) startAngle = 45.0F; // 从 startAngle 参数到弧线的结束点沿顺时针方向度量的角(以度为单位) sweepAngle = 180.0F; // 画矩形 g.DrawRectangle(p, rect); // 画弧. g.DrawArc(p, rect, startAngle, sweepAngle); }
效果图:
三、DrawClosedCurve 画闭合曲线
1 private void frmGraphics_Paint(object sender, PaintEventArgs e) 2 { 3 Graphics g = this.CreateGraphics();// e.Graphics; 4 5 //DrawClosedCurve 画闭合曲线 6 Pen redPen = new Pen(Color.Red, 3); 7 Pen greenPen = new Pen(Color.Green, 3); 8 9 // 创建七个点来定义一条曲线. 10 Point point1 = new Point(50, 50); 11 Point point2 = new Point(100, 25); 12 Point point3 = new Point(200, 5); 13 Point point4 = new Point(250, 50); 14 Point point5 = new Point(300, 100); 15 Point point6 = new Point(350, 200); 16 Point point7 = new Point(250, 250); 17 Point[] curvePoints = 18 { 19 point1, 20 point2, 21 point3, 22 point4, 23 point5, 24 point6, 25 point7 26 }; 27 28 // 画线. 29 e.Graphics.DrawLines(redPen, curvePoints); 30 31 // 画闭合曲线 32 e.Graphics.DrawClosedCurve(greenPen, curvePoints); 33 }
效果图:
四、DrawCurve 画曲线
1 private void frmGraphics_Paint(object sender, PaintEventArgs e) 2 { 3 Graphics g = this.CreateGraphics();// e.Graphics; 4 5 // Create pens. 6 Pen redPen = new Pen(Color.Red, 3); 7 Pen greenPen = new Pen(Color.Green, 3); 8 9 // Create points that define curve. 10 Point point1 = new Point(50, 50); 11 Point point2 = new Point(100, 25); 12 Point point3 = new Point(200, 5); 13 Point point4 = new Point(250, 50); 14 Point point5 = new Point(300, 100); 15 Point point6 = new Point(350, 200); 16 Point point7 = new Point(250, 250); 17 Point[] curvePoints = { point1, point2, point3, point4, point5, point6, point7 }; 18 19 // Draw lines between original points to screen. 20 e.Graphics.DrawLines(redPen, curvePoints); 21 22 // Draw curve to screen. 23 e.Graphics.DrawCurve(greenPen, curvePoints); 24 }
效果图:
五、DrawEllipse 画椭圆
1 private void frmGraphics_Paint(object sender, PaintEventArgs e) 2 { 3 Graphics g = this.CreateGraphics();// e.Graphics; 4 5 // Create pen. 6 Pen blackPen = new Pen(Color.Black, 3); 7 8 // 创建矩形,定义椭圆边界值 9 Rectangle rect = new Rectangle(0, 0, 200, 100); 10 11 // 绘制椭圆 12 e.Graphics.DrawEllipse(blackPen, rect); 13 }
效果图:
六、DrawImage 画图像
1 private void frmGraphics_Paint(object sender, PaintEventArgs e) 2 { 3 Pen p = new Pen(Color.Red); 4 // 创建Image对象 5 Image newImage = Image.FromFile(@"E:TestTestGDI_DemoimagesDrawBackgroundImage.png"); 6 7 // 创建图像左上角的坐标 8 float x = 100.0F; 9 float y = 100.0F; 10 11 // 定义矩形的位置(这个和Graphics对象的方法有关)和大小 12 // public RectangleF (float x, float y, float width, float height); 13 // x,y 矩形左上角坐标 14 RectangleF srcRect = new RectangleF(50.0F, 50.0F, 150.0F, 150.0F); 15 // 单位:像素 16 GraphicsUnit units = GraphicsUnit.Pixel; 17 18 // 矩形左上角坐标,是参照Form窗体的 19 e.Graphics.DrawRectangle(p, 50.0F, 50.0F, 150.0F, 150.0F); 20 // 将图像绘制到屏幕上 21 // 矩形左上角坐标,是参照Image对象的 22 e.Graphics.DrawImage(newImage, x, y, srcRect, units); 23 }
效果图:
说明:
七、DrawLine 画线
1 private void frmGraphics_Paint(object sender, PaintEventArgs e) 2 { 3 Pen p = new Pen(Color.Red); 4 //起点坐标 5 Point point1 = new Point(100, 100); 6 //终点坐标 7 Point point2 = new Point(500, 100); 8 9 // 绘制直线 10 e.Graphics.DrawLine(p, point1, point2); 11 }
效果图:
1 private void frmGraphics_Paint(object sender, PaintEventArgs e) 2 { 3 Pen p = new Pen(Color.Red); 4 Point[] points = 5 { 6 new Point(10, 10), 7 new Point(10, 100), 8 new Point(200, 50), 9 new Point(250, 300) 10 }; 11 12 //Draw lines to screen. 13 e.Graphics.DrawLines(p, points); 14 }
效果图:
八、DrawPath 通过路径画线和曲线
1 private void frmGraphics_Paint(object sender, PaintEventArgs e) 2 { 3 Pen p = new Pen(Color.Red); 4 // 创建图形路径对象,并向它添加一个椭圆 5 GraphicsPath graphPath = new GraphicsPath(); 6 graphPath.AddEllipse(0, 0, 200, 100); 7 // 绘制图形路径 8 e.Graphics.DrawPath(p, graphPath); 9 }
效果图:
九、DrawPie 画饼形
1 private void frmGraphics_Paint(object sender, PaintEventArgs e) 2 { 3 Pen p = new Pen(Color.Red); 4 // 绘制一个扇形,该形状由一个坐标对、宽度、高度以及两条射线所指定的椭圆定义 5 float x = 0.0F; 6 float y = 0.0F; 7 float width = 200.0F; 8 float height = 100.0F; 9 10 // Create start and sweep angles. 11 float startAngle = 0.0F; 12 float sweepAngle = 90.0F; 13 14 // Draw pie to screen. 15 e.Graphics.DrawPie(p, x, y, width, height, startAngle, sweepAngle); 16 }
效果图 |
说明图 |
|
|
十、DrawPolygon 画多边形
1 private void frmGraphics_Paint(object sender, PaintEventArgs e) 2 { 3 Pen p = new Pen(Color.Red,3); 4 // Create points that define polygon. 5 PointF point1 = new PointF(50.0F, 50.0F); 6 PointF point2 = new PointF(100.0F, 25.0F); 7 PointF point3 = new PointF(200.0F, 5.0F); 8 PointF point4 = new PointF(250.0F, 50.0F); 9 PointF point5 = new PointF(300.0F, 100.0F); 10 PointF point6 = new PointF(350.0F, 200.0F); 11 PointF point7 = new PointF(250.0F, 250.0F); 12 PointF[] curvePoints = 13 { 14 point1, 15 point2, 16 point3, 17 point4, 18 point5, 19 point6, 20 point7 21 }; 22 23 // Draw polygon curve to screen. 24 e.Graphics.DrawPolygon(p, curvePoints); 25 }
效果图:
十一、DrawRectangle 画矩形
1 private void frmGraphics_Paint(object sender, PaintEventArgs e) 2 { 3 Pen p = new Pen(Color.Red,3); 4 // Create rectangle. 5 Rectangle rect = new Rectangle(0, 0, 200, 200); 6 7 // Draw rectangle to screen. 8 e.Graphics.DrawRectangle(p, rect); 9 }
效果图:
十二、DrawString 绘制文字
1 private void frmGraphics_Paint(object sender, PaintEventArgs e) 2 { 3 // 定义绘制文字 4 String drawString = "Sample Text"; 5 6 // 定义字体 7 Font drawFont = new Font("Arial", 16); 8 // 定义单色画笔,墨要使用刷子(可以理解为毛笔)才能作画 9 // 所以刷子就有以下几种 10 // SolidBrush : 单色画笔 11 // TextureBrush : 纹理画笔(使用图像来填充形状的内部) 12 // HatchBrush : 用阴影样式、前景色和背景色定义矩形画笔 13 // LinearGradientBrush : 线性渐变 14 // PathGradientBrush : 通过渐变填充 GraphicsPath 对象 15 SolidBrush drawBrush = new SolidBrush(Color.Black); 16 17 // Create point for upper-left corner of drawing. 18 float x = 150.0F; 19 float y = 50.0F; 20 21 // 绘制文本的格式化特性(如行距和对齐方式) 22 StringFormat drawFormat = new StringFormat(); 23 drawFormat.FormatFlags = StringFormatFlags.DirectionVertical; 24 25 // Draw string to screen. 26 e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat); 27 }
效果图:
十三、FillEllipse 填充椭圆
1 private void frmGraphics_Paint(object sender, PaintEventArgs e) 2 { 3 // 定义单色画笔,墨要使用刷子(可以理解为毛笔)才能作画 4 // 所以刷子就有以下几种 5 // SolidBrush : 单色画笔 6 // TextureBrush : 纹理画笔(使用图像来填充形状的内部) 7 // HatchBrush : 用阴影样式、前景色和背景色定义矩形画笔 8 // LinearGradientBrush : 线性渐变 9 // PathGradientBrush : 通过渐变填充 GraphicsPath 对象 10 SolidBrush redBrush = new SolidBrush(Color.Red); 11 12 // Create location and size of ellipse. 13 int x = 0; 14 int y = 0; 15 int width = 200; 16 int height = 100; 17 18 // 填充边框所定义的椭圆的内部 19 e.Graphics.FillEllipse(redBrush, x, y, width, height); 20 }
效果图:
十四、FillPath 填充路径
1 private void frmGraphics_Paint(object sender, PaintEventArgs e) 2 { 3 // 定义单色画笔,墨要使用刷子(可以理解为毛笔)才能作画 4 // 所以刷子就有以下几种 5 // SolidBrush : 单色画笔 6 // TextureBrush : 纹理画笔(使用图像来填充形状的内部) 7 // HatchBrush : 用阴影样式、前景色和背景色定义矩形画笔 8 // LinearGradientBrush : 线性渐变 9 // PathGradientBrush : 通过渐变填充 GraphicsPath 对象 10 11 12 //SolidBrush redBrush = new SolidBrush(Color.Red); 13 14 Bitmap image1 = (Bitmap)Image.FromFile(@"E:TestTestGDI_DemoimagesTextureImage.png", true); 15 16 TextureBrush texture = new TextureBrush(image1); 17 texture.WrapMode = System.Drawing.Drawing2D.WrapMode.Tile; 18 19 20 // 创建图形路径对象 21 GraphicsPath graphPath = new GraphicsPath(); 22 //向图形路径添加一个椭圆。 23 graphPath.AddEllipse(0, 0, 200, 100); 24 25 // 填充 GraphicsPath 26 e.Graphics.FillPath(texture, graphPath); 27 }
效果图:
十五、FillPie 填充饼图
1 private void frmGraphics_Paint(object sender, PaintEventArgs e) 2 { 3 // 定义单色画笔,墨要使用刷子(可以理解为毛笔)才能作画 4 // 所以刷子就有以下几种 5 // SolidBrush : 单色画笔 6 // TextureBrush : 纹理画笔(使用图像来填充形状的内部) 7 // HatchBrush : 用阴影样式、前景色和背景色定义矩形画笔 8 // LinearGradientBrush : 线性渐变 9 // PathGradientBrush : 通过渐变填充 GraphicsPath 对象 10 11 12 //SolidBrush redBrush = new SolidBrush(Color.Red); 13 14 // HatchStyle.Horizontal => 水平线, Color.Red => 红色水平线,背景色:Color.FromArgb(255, 128, 255, 255) 15 HatchBrush hBrush = new HatchBrush(HatchStyle.Horizontal, Color.Red, Color.FromArgb(255, 128, 255, 255)); 16 17 Rectangle rect = new Rectangle(0, 0, 200, 100); 18 // Create start and sweep angles. 19 float startAngle = 0.0F; 20 float sweepAngle = 45.0F; 21 22 // Fill pie to screen. 23 e.Graphics.FillPie(hBrush, rect, startAngle, sweepAngle); 24 }
效果图:
十六、FillPolygon 填充多边形
1 private void frmGraphics_Paint(object sender, PaintEventArgs e) 2 { 3 // 定义单色画笔,墨要使用刷子(可以理解为毛笔)才能作画 4 // 所以刷子就有以下几种 5 // SolidBrush : 单色画笔 6 // TextureBrush : 纹理画笔(使用图像来填充形状的内部) 7 // HatchBrush : 用阴影样式、前景色和背景色定义矩形画笔 8 // LinearGradientBrush : 线性渐变 9 // PathGradientBrush : 通过渐变填充 GraphicsPath 对象 10 11 12 //SolidBrush redBrush = new SolidBrush(Color.Red); 13 14 //坐标相对填充对象 15 LinearGradientBrush linGrBrush = new LinearGradientBrush(new Point(0, 10), new Point(200, 10), 16 Color.FromArgb(255, 255, 0, 0), // Opaque red 17 Color.FromArgb(255, 0, 0, 255)); // Opaque blue 18 19 Pen pen = new Pen(linGrBrush,3); 20 21 e.Graphics.DrawLine(pen, 0, 10, 200, 10); 22 23 // Create points that define polygon. 24 Point point1 = new Point(50, 50); 25 Point point2 = new Point(100, 25); 26 Point point3 = new Point(200, 5); 27 Point point4 = new Point(250, 50); 28 Point point5 = new Point(300, 100); 29 Point point6 = new Point(350, 200); 30 Point point7 = new Point(250, 250); 31 Point[] curvePoints = { point1, point2, point3, point4, point5, point6, point7 }; 32 33 // Draw polygon to screen. 34 e.Graphics.FillPolygon(linGrBrush, curvePoints); 35 }
效果图:
十七、FillRectangle 填充矩形
1 private void frmGraphics_Paint(object sender, PaintEventArgs e) 2 { 3 // 定义单色画笔,墨要使用刷子(可以理解为毛笔)才能作画 4 // 所以刷子就有以下几种 5 // SolidBrush : 单色画笔 6 // TextureBrush : 纹理画笔(使用图像来填充形状的内部) 7 // HatchBrush : 用阴影样式、前景色和背景色定义矩形画笔 8 // LinearGradientBrush : 线性渐变 9 // PathGradientBrush : 通过渐变填充 GraphicsPath 对象 10 11 12 SolidBrush redBrush = new SolidBrush(Color.Red); 13 Rectangle rect = new Rectangle(0, 0, 200, 200); 14 e.Graphics.FillRectangle(redBrush, rect); 15 }
效果图:
十八、FillRectangles 填充矩形组
1 private void frmGraphics_Paint(object sender, PaintEventArgs e) 2 { 3 // 定义单色画笔,墨要使用刷子(可以理解为毛笔)才能作画 4 // 所以刷子就有以下几种 5 // SolidBrush : 单色画笔 6 // TextureBrush : 纹理画笔(使用图像来填充形状的内部) 7 // HatchBrush : 用阴影样式、前景色和背景色定义矩形画笔 8 // LinearGradientBrush : 线性渐变 9 // PathGradientBrush : 通过渐变填充 GraphicsPath 对象 10 11 12 //SolidBrush redBrush = new SolidBrush(Color.Red); 13 SolidBrush blueBrush = new SolidBrush(Color.Blue); 14 15 RectangleF[] rects = { new RectangleF(0.0F, 0.0F, 100.0F, 200.0F), 16 new RectangleF(100.0F, 200.0F, 250.0F, 50.0F), 17 new RectangleF(300.0F, 0.0F, 50.0F, 100.0F) }; 18 19 e.Graphics.FillRectangles(blueBrush, rects); 20 }
效果图:
十九、FillRegion 填充区域
1 private void frmGraphics_Paint(object sender, PaintEventArgs e) 2 { 3 // 定义单色画笔,墨要使用刷子(可以理解为毛笔)才能作画 4 // 所以刷子就有以下几种 5 // SolidBrush : 单色画笔 6 // TextureBrush : 纹理画笔(使用图像来填充形状的内部) 7 // HatchBrush : 用阴影样式、前景色和背景色定义矩形画笔 8 // LinearGradientBrush : 线性渐变 9 // PathGradientBrush : 通过渐变填充 GraphicsPath 对象 10 11 Graphics g = e.Graphics; 12 // 创建矩形,为定义Region对象的填充区域 13 Rectangle regionRect = new Rectangle(10, 10, 50, 50); 14 Pen pen1 = new Pen(Color.Black); 15 //为看出效果 16 g.DrawRectangle(pen1, regionRect); 17 18 // 创建第二个矩形,主要用于区域交集填充 19 RectangleF unionRect = new RectangleF(25, 25, 50, 50);//第2个矩形 20 pen1.Color = Color.Red; 21 //为看出效果 22 g.DrawEllipse(pen1, unionRect);//画椭圆 23 24 GraphicsPath myPath = new GraphicsPath(); 25 myPath.AddEllipse(unionRect); 26 27 // 创建Region对象的填充区域 28 Region myRegion = new Region(regionRect); 29 30 //两个区域的交集被填充 31 myRegion.Intersect(myPath); 32 33 SolidBrush blueBrush = new SolidBrush(Color.Blue); 34 // 填充 35 e.Graphics.FillRegion(blueBrush, myRegion); 36 37 //=================Complement================== 38 regionRect = new Rectangle(100, 10, 50, 50); 39 pen1 = new Pen(Color.Black); 40 //为看出效果 41 g.DrawRectangle(pen1, regionRect); 42 43 // 创建第二个矩形 44 unionRect = new RectangleF(115, 25, 50, 50); 45 pen1.Color = Color.Red; 46 g.DrawEllipse(pen1, unionRect);//画椭圆 47 48 myPath = new GraphicsPath(); 49 myPath.AddEllipse(unionRect); 50 51 myRegion = new Region(regionRect); 52 // myPath 无交集的区域被填充 53 myRegion.Complement(myPath); 54 55 // 填充 56 e.Graphics.FillRegion(blueBrush, myRegion); 58 59 //=================Exclude================== 60 61 regionRect = new Rectangle(190, 10, 50, 50); 62 pen1 = new Pen(Color.Black); 63 //为看出效果 64 g.DrawRectangle(pen1, regionRect); 65 66 // 创建第二个矩形 67 unionRect = new RectangleF(205, 25, 50, 50); 68 pen1.Color = Color.Red; 69 g.DrawEllipse(pen1, unionRect);//画椭圆 70 71 myPath = new GraphicsPath(); 72 myPath.AddEllipse(unionRect); 73 74 myRegion = new Region(regionRect); 75 76 // myRegion 无交集的区域被填充 77 myRegion.Exclude(myPath); 78 79 // 填充 80 e.Graphics.FillRegion(blueBrush, myRegion); 81 82 //=================Union================== 83 84 regionRect = new Rectangle(280, 10, 50, 50); 85 pen1 = new Pen(Color.Black); 86 //为看出效果 87 g.DrawRectangle(pen1, regionRect); 88 89 // 创建第二个矩形 90 unionRect = new RectangleF(295, 25, 50, 50); 91 pen1.Color = Color.Red; 92 g.DrawEllipse(pen1, unionRect);//画椭圆 93 94 myPath = new GraphicsPath(); 95 myPath.AddEllipse(unionRect); 96 97 myRegion = new Region(regionRect); 98 99 // 两个区域被填充 100 myRegion.Union(myPath); 101 102 // 填充 103 e.Graphics.FillRegion(blueBrush, myRegion); 104 105 //=================Xor================== 106 107 regionRect = new Rectangle(370, 10, 50, 50); 108 pen1 = new Pen(Color.Black); 109 //为看出效果 110 g.DrawRectangle(pen1, regionRect); 111 112 // 创建第二个矩形 113 unionRect = new RectangleF(385, 25, 50, 50); 114 pen1.Color = Color.Red; 115 g.DrawEllipse(pen1, unionRect);//画椭圆 116 117 myPath = new GraphicsPath(); 118 myPath.AddEllipse(unionRect); 119 120 myRegion = new Region(regionRect); 121 122 // 交集以外区域被填充 123 myRegion.Xor(myPath); 124 125 // 填充 126 e.Graphics.FillRegion(blueBrush, myRegion); 127 }
效果图: