1、在实际应用中,很多图形是由点和线构成的, 的 Graphics 类提供了很多画线图的方法,我们可以使用这些重载了的方法组合出丰富的图案来。下面我们就来了解下这些绘线图的方法。一、DrawLine 方法该方法绘制一条连接由坐标对指定的两个点的线条,所以我们需要指定这两个点的坐标,如下所示:DrawLine(pen,x1,y1,x2,y2)或者 DrawLine(pen,Point1, Point2)下面示例将在 PictureBox 控件表面绘制一条直线:Dim g As Graphics = Me.PictureBox1.CreateGraphicsDim mpen As New Pen(Co
2、lor.Red)g.DrawLine(mpen, 0, 80, 0, 160)也可以使用如下代码来实现一样的效果:Dim g As Graphics = Me.PictureBox1.CreateGraphicsDim mpen As New Pen(Color.Red)Dim p1 As Point = New Point(0, 80)Dim p2 As Point = New Point(0, 160)g.DrawLine(mpen, p1, p2)当然,我们也可以使用 DrawLine 方法画出更加复杂的图形,如下代码所示:Private Sub Button1_Click(ByVal
3、sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim g As Graphics = Me.PictureBox1.CreateGraphicsDim mpen As New Pen(Color.Red)Dim i As IntegerFor i = 0 To 160 Step 10g.DrawLine(mpen, 0, 80, i, 0)g.DrawLine(mpen, 0, 80, i, 160)g.DrawLine(mpen, 160, 80, 160 - i, 160)g.DrawL
4、ine(mpen, 160, 80, 160 - i, 0)NextEnd Sub运行后效果如下图一所示:图一:使用 DrawLine 方法画复杂图形二、DrawLines 方法该方法绘制一系列连接一组 Point 结构的线段,它按顺序把每个点都连接起来,构成一段曲线,DrawLines 的形式如下:DrawLines(Pen, Point()Point()参数就是一个由一系列的点构成的数组。如下代码所示,我们要在 PictureBox 控件中绘制一条由四个不同的坐标点构成的曲线:Private Sub Button1_Click(ByVal sender As System.Object,
5、ByVal e As System.EventArgs) Handles Button1.ClickDim g As Graphics = Me.PictureBox1.CreateGraphicsDim mpen As New Pen(Color.Red)Dim Points As PointF() = New PointF(0.0F, 0.0F), New PointF(10.0F, 15.0F), New PointF(20.0F, 20.0F), New PointF(50.0F, 100.0F)g.DrawLines(mpen, points)End Sub运行效果如下图二所示:图二
6、:使用 DrawLines 方法画曲线如图曲线的起点、两个折点、终点就是我们 Pionts 变量中指定的四个点。该方法比较适用于连接如股票的波动线等由大量的点构成的波动曲线。三、DrawEllipse 方法使用 DrawEllipse 方法可以绘制一个由边框定义的椭圆,该边框是由椭圆的左上角坐标,高度以及宽度决定的。DrawEllipse 方法的常用形式:DrawEllipse(x,y,width,height)x,y 为椭圆的左上角坐标点, width 为椭圆的矩形宽,height 为椭圆的矩形高如下代码所示,我们在 PictureBox 控件中绘制一个红色的椭圆:Private Sub B
7、utton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim g As Graphics = Me.PictureBox1.CreateGraphicsDim mpen As New Pen(Color.Red)g.DrawEllipse(mpen, 0, 0, 150, 150)End Subhttp:/ DrawEllipse 方法画椭圆在以上示例中,我们绘制出来的是一个正圆,正圆实际上就是椭圆的特殊表现。四、DrawArc 方法用 DrawArc 方法可以
8、一段椭圆弧,方法格式如下:DrawArc(Pen, x, y, Swidth, height, StartAngle, SweepAngle)x, y, Swidth, height 这四个参数指定了椭圆的结构StartAngle 为椭圆弧的起始角度,该角度是在指以椭圆的圆心为坐标原点、X 轴向右为正方向的坐标系中,圆弧起点与 X 轴的夹角。SweepAngle 为圆弧扫过的角度值,以 StartAngle 参数所指定的起点沿顺时针方向扫过的度数。如下示例代码:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
9、 System.EventArgs) Handles Button1.ClickDim g As Graphics = Me.PictureBox1.CreateGraphicsDim mpen As New Pen(Color.Red)g.DrawArc(mpen, 0.0F, 0.0F, 100.0F, 100.0F, 0.0F, 270.0F)End Sub运行效果如下图四(一)所示。我们把g.DrawArc(mpen, 0.0F, 0.0F, 100.0F, 100.0F, 0.0F, 270.0F)改为g.DrawArc(mpen, 0.0F, 0.0F, 100.0F, 100.0
10、F, 90.0F, 270.0F)http:/ DrawArc 方法画椭圆弧图四(二):使用 DrawArc 方法画椭圆弧对比以上两图,我们可以知道,决定圆弧的起始位置的参数为:StartAngle, 而 SweepAngle 决定了圆弧扫过的度数。五、DrawPie 方法DrawPie 方法画的是一个饼状图,它和 DrawArc 方法一样,先画一条椭圆弧,但是它还把两个端点与圆心连接起来,形成一个饼状图,它的格式与 DrawArc 方法一样,这里就不做说明了。如下代码所示:Private Sub Button1_Click(ByVal sender As System.Object, ByV
11、al e As System.EventArgs) Handles Button1.ClickDim g As Graphics = Me.PictureBox1.CreateGraphicsDim mpen As New Pen(Color.Red)g.DrawPie(mpen, 0.0F, 0.0F, 100.0F, 100.0F, 90.0F, 270.0F)End Sub运行后效果如下图五所示:图五:使用 DrawPie 方法画饼状图http:/ 方法我们可以使用 DrawPolygon 方法来绘制任意的多边形,该方法格式如下:DrawPolygon(pen,Points()Point
12、s()为坐标点数组,我们使用下面代码来演示通过 DrawPolygon 方法绘制一个不规则的四边形,代码如下所示:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim g As Graphics = Me.PictureBox1.CreateGraphicsDim mpen As New Pen(Color.Red)定义四边形的四个顶点的坐标Dim p(3) As Pointp(0).X = 100p(0).Y = 15p(1)
13、.X = 80p(1).Y = 90p(2).X = 90p(2).Y = 120p(3).X = 130p(3).Y = 150g.DrawPolygon(mpen, p)End Sub运行后效果如下图六所示:图六:用 DrawPolygon 画一个四边形七、DrawRectangle 方法DrawRectangle 方法可以绘制由坐标对、宽度和高度指定的矩形。它的格式如下:DrawRectangle(pen, x, y, width, height)其中 X、Y 为矩形的左上角的 XY 坐标点,width, height 分别为矩形的宽和高。下面示例代码是在 PictureBox 中绘制一
14、个矩形:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim g As Graphics = Me.PictureBox1.CreateGraphicsDim mpen As New Pen(Color.Red)g.DrawRectangle(mpen, 20, 30, 100, 80)End Sub运行后效果如下图七所示:图七:画一个矩形 http:/ 方法DrawRectangles 方法和 DrawRectangle 方法
15、一样都是用于画矩形的,但是DrawRectangles 方法是绘制一系列由 Rectangle 结构指定的矩形。其格式如下:DrawRectangles(Pen, Rectangle()如下代码所示,我们在 PictureBox 中绘制三个矩形:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim g As Graphics = Me.PictureBox1.CreateGraphicsDim mpen As New Pen(C
16、olor.Red)Dim rects As RectangleF() = New RectangleF(10.0F, 10.0F, 140.0F, 120.0F), New RectangleF(20.0F, 20.0F, 100.0F, 80.0F), New RectangleF(30.0F, 30.0F, 50.0F, 40.0F)g.DrawRectangles(mpen, rects)End Sub运行效果如下图所示:图八:画三个矩形九、DrawBezier 方法DrawBezier 方法用于绘制由 4 个 Point 结构定义的贝塞尔曲线。它的格式如下:DrawBezier(Pen
17、, x1, y1, x2, y2, x3, y3, x4, y4)x1, y1 为起点,x2, y2 和 x3, y3 为控制点,x4, y4 为终点下面代码示例使用 DrawBezier 方法:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim g As Graphics = Me.PictureBox1.CreateGraphicsDim mpen As New Pen(Color.Red)g.DrawBezier(mpe
18、n, 10, 10, 80, 60, 100, 70, 10, 130)End Sub图九:画一条贝塞尔曲线http:/ 方法 DrawBeziers 方法用 Point 结构数组绘制一系列贝塞尔样条。Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim g As Graphics = Me.PictureBox1.CreateGraphicsDim mpen As New Pen(Color.Red)为第一条曲线创建起始点、结
19、束点和两个控制点;为第二条曲线创建结束点和两个控制点。Dim start As New PointF(10.0F, 10.0F)Dim control1 As New PointF(120.0F, 10.0F)Dim control2 As New PointF(150.0F, 50.0F)Dim end1 As New PointF(150.0F, 10.0F)Dim control3 As New PointF(60.0F, 50.0F)Dim control4 As New PointF(50.0F, 20.0F)Dim end2 As New PointF(50.0F, 130.0F)Dim bezierPoints As PointF() = start, control1, control2, end1, control3, control4, end2g.DrawBeziers(mpen, bezierPoints)End Sub运行效果图如下:图十:画两条贝塞尔曲线注意:这里的两条曲线的起点是一样的。本章我们主要是了解使用 Graphics 对象的方法来绘制线图,在所有绘制线图的方法中,我们都是需要创建笔对象 Pen 的。