'先建立一个bitmap对象,指向图像文件
成都创新互联公司主要从事网站建设、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务永福,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792
Dim pic As Bitmap = New Bitmap("e:\1.jpg")
'定义一个图片框的graphics对象
Dim g As Graphics = PictureBox1.CreateGraphics()
'在图片框上绘图
g.DrawImage(pic, 0, 0, pic.Width, pic.Height)
'释放bitmap对象
pic.Dispose()
'现在即可对文件进行操作
vb点虐 没有自动重画功能,要在Paint事件中写代码对图形重画。
另外一种情况,如果在Image属性设置了一幅图像,图像能够保持完整性的。所以你可以把图形绘在位图上,把位图绑定到Image属性上。
先绑定一幅位图:
Dim bm as New BitMap(800,600)
PictureBox1.Image=bm
作图时不是对图片框,而是在位图上作图。
dim gr As Grapthics=Graphics.FromImage(bm) '建立位图的绘图设备
接下来就可用gr 的绘图方法作图
作完图,PictureBox1.Refresh 刷新一下。
Dim x1 As Single, y1 As Single, s As Integer
Const pi = 3.14159265
Private Sub Form_Load()
Command1.Caption = "圆"
Command2.Caption = "矩形"
Command3.Caption = "三角"
Command4.Caption = "五角"
Command5.Caption = "清"
End Sub
Private Sub Command1_Click()
s = 1
End Sub
Private Sub Command2_Click()
s = 2
End Sub
Private Sub Command3_Click()
s = 3
End Sub
Private Sub Command4_Click()
s = 4
End Sub
Private Sub Command5_Click()
Picture1.Cls
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = 1 Then x1 = x: y1 = y
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = 1 Then
Picture1.AutoRedraw = False
Picture1.Refresh
Picture1.PSet (x1, y1)
Select Case s
Case 1
Picture1.Circle (x1, y1), Sqr((x - x1) ^ 2 + (y - y1) ^ 2)
Case 2
Picture1.Line (x1, y1)-(x, y), , B
Case 3
duobianxing x1, y1, x, y, 3, 60
Case 4
duobianxing x1, y1, x, y, 5, 36
End Select
End If
End Sub
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = 1 Then
Picture1.AutoRedraw = True
Select Case s
Case 1
Picture1.Circle (x1, y1), Sqr((x - x1) ^ 2 + (y - y1) ^ 2)
Case 2
Picture1.Line (x1, y1)-(x, y), , B
Case 3
duobianxing x1, y1, x, y, 3, 60
Case 4
duobianxing x1, y1, x, y, 5, 36
End Select
End If
End Sub
'duobianxing函数参数:
'zhongxinX-多边形中心的横坐标
'zhongxinY-多边形中心的纵坐标
'dingdianX-多边形一个顶点的横坐标
'dingdianY-多边形一个顶点的纵坐标
'bianhuoxing-正多边形的边数或者星形的星角数,例如biaohuoxing=5,dingjiao=72则为正五边形,若dingjiao=36则为五角星(该函数将正多边形按相邻顶角点间的折点成直线的特殊星形绘制)
'dingjiao-多边形的顶角的角度(角度制°)
Function duobianxing(ByVal zhongxinX As Single, ByVal zhongxinY As Single, ByVal dingdianX As Single, ByVal dingdianY As Single, ByVal bianhuoxing As Integer, ByVal dingjiao As Single)
If bianhuoxing = 0 Then Exit Function
l = Sqr((zhongxinX - dingdianX) ^ 2 + (zhongxinY - dingdianY) ^ 2) '星形中心到顶角距离
t1 = Abs(Tan((dingjiao / 2) * pi / 180)) '星形顶角的1/2求正切
t2 = Abs(Tan((360 / (2 * bianhuoxing)) * pi / 180)) '星形每条边所对应的中心角的1/2求正切
r = l * t2 / (t1 + t2) / Cos((dingjiao / 2) * pi / 180) '星形边长
If zhongxinX = dingdianX Then '求星形中心到顶角这条线的角度j
j = IIf(dingdianY zhongxinY, 90, -90)
Else
j = Atn((zhongxinY - dingdianY) / (dingdianX - zhongxinX)) * 180 / pi
If dingdianX zhongxinX Then
If dingdianY zhongxinY Then
j = j - 180
Else
j = j + 180
End If
End If
End If
j1 = j - dingjiao / 2 '边偏离初始角1
j2 = 360 / bianhuoxing + dingjiao + j - dingjiao / 2 '边偏离初始角2(如果是正多边形j1=j2)
px1 = dingdianX: py1 = dingdianY '指定星形的第一个顶点
For i = 1 To bianhuoxing * 2
If i Mod 2 = 0 Then
px2 = px1 + r * Cos(j2 * pi / 180): py2 = py1 - r * Sin(j2 * pi / 180) '指定星形下一个顶点
j2 = j2 + 360 / bianhuoxing '边偏角+2个边长对应的中心角
Else
px2 = px1 - r * Cos(j1 * pi / 180): py2 = py1 + r * Sin(j1 * pi / 180)
j1 = j1 + 360 / bianhuoxing
End If
Picture1.Line (px1, py1)-(px2, py2) '画线
px1 = px2: py1 = py2
Next
End Function