第一種方法:如果知道圖片的URL,那么可以直接在Picture1中顯示:
Private Declare Function OleLoadPicturePath Lib "oleaut32.dll" (ByVal szURLorPath As Long, ByVal punkCaller As Long, ByVal dwReserved As Long, ByVal clrReserved As OLE_COLOR, ByRef riid As TGUID, ByRef ppvRet As IPicture) As Long
Private Type TGUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Public Function LoadPicture(ByVal strFileName As String) As Picture
Dim IID As TGUID
With IID
.Data1 = &H7BF80980
.Data2 = &HBF32
.Data3 = &H101A
.Data4(0) = &H8B
.Data4(1) = &HBB
.Data4(2) = &H0
.Data4(3) = &HAA
.Data4(4) = &H0
.Data4(5) = &H30
.Data4(6) = &HC
.Data4(7) = &HAB
End With
On Error GoTo LocalErr
OleLoadPicturePath StrPtr(strFileName), 0&, 0&, 0&, IID, LoadPicture
Exit Function
LocalErr:
Set LoadPicture = VB.LoadPicture(strFileName)
Err.Clear
End Function
Private Sub Command1_Click()
Picture1.Picture = LoadPicture("http://www.baidu.com/img/bdlogo.gif")
End Sub
第二種方法:如果知道圖片的URL,那么可以先把圖片下載到本地,再在Picture1中顯示:
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Private Sub Command1_Click()
URLDownloadToFile 0&, "http://www.baidu.com/img/bdlogo.gif", App.Path & "\bdlogo.gif", 0&, 0&
Picture1.Picture = LoadPicture(App.Path & "\bdlogo.gif")
End Sub
第三種方法:如果不知道圖片URL,但知道網(wǎng)頁的URL,那么可以用WebBrowser控件顯示網(wǎng)頁,再從中找到需要的圖片顯示到Picture1中:
Private Sub Command1_Click()
Dim CtrlRange As Object, img As Object
Set CtrlRange = WebBrowser1.Document.body.createControlRange
For Each img In WebBrowser1.Document.images
If InStr(img.src, "bdlogo") Then
CtrlRange.Add img
CtrlRange.execCommand "Copy"
Picture1.Picture = Clipboard.GetData
Exit For
End If
Next
End Sub
Private Sub Form_Load()
WebBrowser1.Navigate "http://www.baidu.com"
End Sub
以上方法都可以顯示百度首頁的那個Logo圖片。相比而言,第一種方法是最好的,別看它代碼最多,但它是完全用代碼實現(xiàn)的,沒有借助其他控件,也沒有產(chǎn)生中間文件,用起來就好像網(wǎng)上的圖片就是自己硬盤中的圖片一樣;第二種方法代碼最簡單,但它會產(chǎn)生硬盤文件,不過如果你恰巧需要下載文件的(就是說不但要顯示,還想利用這個圖片做其他操作),那么這個方法就是最佳選擇了;第三種方法最復(fù)雜,需要用到網(wǎng)頁控件,而且要用剪貼板過渡,不過如果你的軟件本來就是需要用到網(wǎng)頁控件的,或者你想獲得同一網(wǎng)頁的多張圖片,那么這又是不二選擇了。
聯(lián)系客服