'先建立一個(gè)bitmap對象,指向圖像文件
成都創(chuàng)新互聯(lián)公司主要從事網(wǎng)站建設(shè)、成都網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)永福,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792
Dim pic As Bitmap = New Bitmap("e:\1.jpg")
'定義一個(gè)圖片框的graphics對象
Dim g As Graphics = PictureBox1.CreateGraphics()
'在圖片框上繪圖
g.DrawImage(pic, 0, 0, pic.Width, pic.Height)
'釋放bitmap對象
pic.Dispose()
'現(xiàn)在即可對文件進(jìn)行操作
vb點(diǎn)虐 沒有自動(dòng)重畫功能,要在Paint事件中寫代碼對圖形重畫。
另外一種情況,如果在Image屬性設(shè)置了一幅圖像,圖像能夠保持完整性的。所以你可以把圖形繪在位圖上,把位圖綁定到Image屬性上。
先綁定一幅位圖:
Dim bm as New BitMap(800,600)
PictureBox1.Image=bm
作圖時(shí)不是對圖片框,而是在位圖上作圖。
dim gr As Grapthics=Graphics.FromImage(bm) '建立位圖的繪圖設(shè)備
接下來就可用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函數(shù)參數(shù):
'zhongxinX-多邊形中心的橫坐標(biāo)
'zhongxinY-多邊形中心的縱坐標(biāo)
'dingdianX-多邊形一個(gè)頂點(diǎn)的橫坐標(biāo)
'dingdianY-多邊形一個(gè)頂點(diǎn)的縱坐標(biāo)
'bianhuoxing-正多邊形的邊數(shù)或者星形的星角數(shù),例如biaohuoxing=5,dingjiao=72則為正五邊形,若dingjiao=36則為五角星(該函數(shù)將正多邊形按相鄰頂角點(diǎn)間的折點(diǎn)成直線的特殊星形繪制)
'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)) '星形每條邊所對應(yīng)的中心角的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 '指定星形的第一個(gè)頂點(diǎn)
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) '指定星形下一個(gè)頂點(diǎn)
j2 = j2 + 360 / bianhuoxing '邊偏角+2個(gè)邊長對應(yīng)的中心角
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