Public Class Form1
创新互联建站-专业网站定制、快速模板网站建设、高性价比济水街道网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式济水街道网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖济水街道地区。费用合理售后完善,十余年实体公司更值得信赖。
Public myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Public formGraphics As System.Drawing.Graphics
Private r As Integer = 5 '点半径
Private data(10) As System.Drawing.Point '数据点
Private link(10, 10) As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each pt In data
formGraphics.DrawEllipse(myPen, New Rectangle(pt.X, pt.Y, r, r))
Next
DrawLink(data)
End Sub
Private Sub DrawLink(data() As System.Drawing.Point)
Dim pt1, pt2 As System.Drawing.Point
For i = 0 To UBound(link)
For j = 0 To UBound(link, 2) - i - 1
If link(i, j) = 1 Then
pt1.X = data(i).X + r / 2 : pt1.Y = data(i).Y + r / 2
pt2.X = data(j).X + r / 2 : pt2.Y = data(j).Y + r / 2
formGraphics.DrawLine(myPen, pt1, pt2)
End If
Next j
Next i
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim i, j As Integer
formGraphics = GroupBox1.CreateGraphics()
r = 10
'生成一些测试数据
Randomize()
For i = 0 To UBound(data)
data(i).x = Rnd() * 400
data(i).y = Rnd() * 200
Next
'生成一些测试边
For i = 0 To UBound(link)
For j = 0 To UBound(link, 2) - i - 1
link(i, j) = Int(Rnd() * 2)
Next j
Next i
End Sub
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
myPen.Dispose()
formGraphics.Dispose()
End Sub
End Class
VB.net与VB不同。
VB.net已经有专门绘图的类。
可以定义笔刷然后用Drawing类中的方法绘制。
Private Sub DrawEllipse()
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics as System.Drawing.Graphics
formGraphics = Me.CreateGraphics()
formGraphics.DrawEllipse(myPen, New Rectangle(0,0,200,300))
myPen.Dispose()
formGraphics.Dispose()
End Sub
Private Sub DrawRectangle()
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics as System.Drawing.Graphics
formGraphics = Me.CreateGraphics()
formGraphics.DrawRectangle(myPen, New Rectangle(0,0,200,300))
myPen.Dispose()
formGraphics.Dispose()
End Sub
参考一下下面这段代码:
‘ 首先picturebox1 加载一张图像
FolderBrowserDialog1.Description = "选择图片文件夹导入图片"
FolderBrowserDialog1.ShowDialog()
path = FolderBrowserDialog1.SelectedPath()
If path = "" Then Return
strSrcFile = Dir(path "\*.tif")
PictureBox1.Image = Image.FromFile(path "\" strSrcFile)
’ 然后再在picturebox1中用graphic画图而不清空原图像
' 建立一个画图对象
Dim g As Graphics = Me.PictureBox1.CreateGraphics
‘ 定义画笔
Dim myPen As System.Drawing.Pen = New System.Drawing.Pen(Color.Blue)
’ 画出矩形框并且填充颜色(颜色保持50%的透明度,使得下面原来的图片背景能看得到)
g.DrawRectangle(myPen, New System.Drawing.Rectangle(50, 50, 30, 20))
g.FillRectangle(New SolidBrush(Color.FromArgb(50, Color.YellowGreen)), New System.Drawing.Rectangle(50, 50, 30, 20))
' 最后释放画图对象
g.Dispose()
效果大致如下图所示:
不用PictureBoxTest.Image属性,直接把图形绘制到PictureBoxTest上面就可以了。
Dim button As Integer = 0
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles Button1.Click
Using g As Graphics = Graphics.FromHwnd(PictureBoxTest.Handle)
Dim penRed As Pen = New Pen(Color.Red, 1) '定义红色画笔
Dim penblue As Pen = New Pen(Color.Blue, 1) '定义蓝色画笔
If button = 0 Then
g.DrawLine(penRed, 0, 0, 100, 100)
button = 1
ElseIf button = 1 Then
g.DrawLine(penblue, 100, 100, 200, 200)
button = 0
End If
End Using
End Sub