Graphics用来绘图。
用draw来划线。vb.net没有画点的功能。
可以画直接、矩形、椭圆、Bezier、弧、饼(封闭的弧线)。
要注意的是:弧、饼用的是度数(不是弧度),且是顺时间转的角度。
Imports System.Drawing
Imports System.Math
Public Class Form1
Const pi As Double = 3.14159
'画点(用一个像素的矩形或两个像素的直接来模拟
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim gr As Graphics = PictureBox1.CreateGraphics
Dim j As Int32 = 50
For i As Int32 = 10 To 150
gr.DrawLine(Pens.Red, i, j, i + 1, j + 1)
j += 1
Next
End Sub
'用直线构成图
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
PictureBox1.Refresh() '清除
Dim gr As Graphics = PictureBox1.CreateGraphics
Dim x1, y1, x2, y2, d As Short '起点终点、基准距离
Dim b, c As Double '真实距离
d = 60
For a = 0 To 2 * pi Step pi / 360
b = d * (1 + 1 / 4 * Cos(12 * a))
c = b * (1 + Sin(4 * a))
x1 = 150 + Int(c * Cos(a)) '(150,180)中心点
y1 = 180 + Int(c * Sin(a))
x2 = 150 + Int(c * Cos(a + pi / 5))
y2 = 180 + Int(c * Sin(a + pi / 5))
gr.DrawLine(Pens.BlueViolet, x1, y1, x2, y2)
Next
End Sub
'椭圆或圆
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
PictureBox1.Refresh()
Dim gr As Graphics = PictureBox1.CreateGraphics
gr.DrawEllipse(Pens.Red, New Rectangle(0, 0, 100, 80))
gr.DrawEllipse(Pens.Aqua, New Rectangle(120, 120, 80, 80))
End Sub
'椭圆弧 或圆弧 (是对上面椭圆的部分提取)
'注意:开始和结束角度是以度为单位,且顺时针方向,下面是从30度到120度
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
PictureBox1.Refresh()
Dim gr As Graphics = PictureBox1.CreateGraphics
gr.DrawArc(Pens.Red, New Rectangle(20, 20, 150, 200), 30, 120)
End Sub
'封闭椭圆弧线,即饼图
'参数与arc同,只是成图时是封闭的
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
PictureBox1.Refresh()
Dim gr As Graphics = PictureBox1.CreateGraphics
gr.DrawPie(Pens.Red, New Rectangle(20, 20, 150, 200), 30, 120)
End Sub
'贝赛尔曲线 Bezier
'它由四个点:起点、两个中间点(控制)、终点 组成
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
PictureBox1.Refresh()
Dim gr As Graphics = PictureBox1.CreateGraphics
gr.DrawBezier(Pens.Brown, 10, 20, 140, 40, 40, 240, 200, 270)
gr.DrawRectangle(Pens.Red, New Rectangle(200, 40, 80, 170))
End Sub
'多边形--不规则形状
'由多个点来决定,多个点用Points()数组来表达
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
PictureBox1.Refresh()
Dim gr As Graphics = PictureBox1.CreateGraphics
Dim p(2) As System.Drawing.Point
p(0).X = 30 : p(0).Y = 30
p(1).X = 100 : p(1).Y = 100
p(2).X = 50 : p(2).Y = 250
gr.DrawPolygon(Pens.DarkGreen, p)
End Sub
End Class