在实际应用中,很多图形是由点和线构成的,vb.net的Graphics 类提供了很多画线图的方法,我们可以使用这些重载了的方法组合出丰富的图案来。
下面我们就来了解下这些绘线图的方法。
一、DrawLine 方法
该方法绘制一条连接由坐标对指定的两个点的线条,所以我们需要指定这两个点的坐标,如下所示:
DrawLine(pen,x1,y1,x2,y2)或者DrawLine(pen,Point1,Point2)
下面示例将在PictureBox控件表面绘制一条直线:
Dim g As Graphics = Me.PictureBox1.CreateGraphics
Dim mpen As New Pen(Color.Red)
g.DrawLine(mpen,80,160)
Dim g As Graphics = Me.PictureBox1.CreateGraphics
Dim 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 sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
Dim g As Graphics = Me.PictureBox1.CreateGraphics
Dim mpen As New Pen(Color.Red)
Dim i As Integer
For i = 0 To 160 Step 10
g.DrawLine(mpen,i,0)
g.DrawLine(mpen,160,160 - i,0)
Next
End Sub
运行后效果如下图一所示:
二、DrawLines 方法
该方法绘制一系列连接一组 Point 结构的线段,它按顺序把每个点都连接起来,构成一段曲线,DrawLines的形式如下:
DrawLines(Pen,Point())
Point()参数就是一个由一系列的点构成的数组。
如下代码所示,我们要在PictureBox 控件中绘制一条由四个不同的坐标点构成的曲线:
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
Dim g As Graphics = Me.PictureBox1.CreateGraphics
Dim 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
运行效果如下图二所示:
如图曲线的起点、两个折点、终点就是我们Pionts变量中指定的四个点。该方法比较适用于连接如股票的波动线等由大量的点构成的波动曲线。
三、DrawEllipse方法
使用 DrawEllipse 方法可以绘制一个由边框定义的椭圆,该边框是由椭圆的左上角坐标,高度以及宽度决定的。
DrawEllipse 方法的常用形式:DrawEllipse(x,y,width,height)
x,y为椭圆的左上角坐标点,width为椭圆的矩形宽,height为椭圆的矩形高
如下代码所示,我们在PictureBox控件中绘制一个红色的椭圆:
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
Dim g As Graphics = Me.PictureBox1.CreateGraphics
Dim mpen As New Pen(Color.Red)
g.DrawEllipse(mpen,150,150)
End Sub
运行效果如下图三所示:
在以上示例中,我们绘制出来的是一个正圆,正圆实际上就是椭圆的特殊表现。
四、DrawArc方法
DrawArc(Pen,x,Swidth,height,StartAngle,SweepAngle)
x,height这四个参数指定了椭圆的结构
StartAngle为椭圆弧的起始角度,该角度是在指以椭圆的圆心为坐标原点、X轴向右为正方向的坐标系中,圆弧起点与X轴的夹角。
SweepAngle 为圆弧扫过的角度值,以StartAngle参数所指定的起点沿顺时针方向扫过的度数。
如下示例代码:
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
Dim g As Graphics = Me.PictureBox1.CreateGraphics
Dim mpen As New Pen(Color.Red)
g.DrawArc(mpen,0.0F,100.0F,270.0F)
End Sub
运行效果如下图四(一)所示。
我们把
g.DrawArc(mpen,270.0F)
改为
g.DrawArc(mpen,90.0F,270.0F)
运行后如下图四(二)所示:
对比以上两图,我们可以知道,决定圆弧的起始位置的参数为:StartAngle,而 SweepAngle 决定了圆弧扫过的度数。
五、DrawPie方法
DrawPie 方法画的是一个饼状图,它和DrawArc方法一样,先画一条椭圆弧,但是它还把两个端点与圆心连接起来,形成一个饼状图,它的格式与DrawArc方法一样,这里就不做说明了。
如下代码所示:
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
Dim g As Graphics = Me.PictureBox1.CreateGraphics
Dim mpen As New Pen(Color.Red)
g.DrawPie(mpen,270.0F)
End Sub
运行后效果如下图五所示:
六、DrawPolygon方法
我们可以使用DrawPolygon方法来绘制任意的多边形,该方法格式如下:
DrawPolygon(pen,Points())
Points()为坐标点数组,我们使用下面代码来演示通过DrawPolygon方法绘制一个不规则的四边形,代码如下所示:
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
Dim g As Graphics = Me.PictureBox1.CreateGraphics
Dim mpen As New Pen(Color.Red)
'定义四边形的四个顶点的坐标
Dim p(3) As Point
p(0).X = 100
p(0).Y = 15
p(1).X = 80
p(1).Y = 90
p(2).X = 90
p(2).Y = 120
p(3).X = 130
p(3).Y = 150
g.DrawPolygon(mpen,p)
End Sub
运行后效果如下图六所示:
七、DrawRectangle方法
DrawRectangle 方法可以绘制由坐标对、宽度和高度指定的矩形。它的格式如下:
DrawRectangle(pen,height)
其中X、Y为矩形的左上角的X\Y坐标点,width,height分别为矩形的宽和高。
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
Dim g As Graphics = Me.PictureBox1.CreateGraphics
Dim mpen As New Pen(Color.Red)
g.DrawRectangle(mpen,20,30,100,80)
End Sub
运行后效果如下图七所示:
八、DrawRectangles方法
DrawRectangles方法和DrawRectangle方法一样都是用于画矩形的,但是DrawRectangles方法是绘制一系列由 Rectangle 结构指定的矩形。其格式如下:
DrawRectangles(Pen,Rectangle())
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
Dim g As Graphics = Me.PictureBox1.CreateGraphics
Dim mpen As New Pen(Color.Red)
Dim rects As RectangleF() = {New RectangleF(10.0F,10.0F,140.0F,120.0F),New RectangleF(20.0F,20.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,y2,x3,y3,x4,y4)
x1,y1为起点,x2,y2和x3,y3为控制点,x4,y4为终点
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
Dim g As Graphics = Me.PictureBox1.CreateGraphics
Dim mpen As New Pen(Color.Red)
g.DrawBezier(mpen,10,60,70,130)
End Sub
十、DrawBeziers方法
DrawBeziers方法用 Point 结构数组绘制一系列贝塞尔样条。
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
Dim g As Graphics = Me.PictureBox1.CreateGraphics
Dim mpen As New Pen(Color.Red)
'为第一条曲线创建起始点、结束点和两个控制点;为第二条曲线创建结束点和两个控制点。
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,end2}
g.DrawBeziers(mpen,bezierPoints)
End Sub
运行效果图如下:
注意:这里的两条曲线的起点是一样的。
本章我们主要是了解使用Graphics对象的方法来绘制线图,在所有绘制线图的方法中,我们都是需要创建笔对象Pen的。下一章我们就继续去了解如何使用Graphics对象来绘制填充图形。
原文链接:https://www.f2er.com/vb/260650.html