如何把网页上的图片导入VB图片框中并显示出来。
展开全部
第一种方法:如果知道图片的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,但知道网页的URL,那么可以用WebBrowser控件显示网页,再从中找到需要的图片显示到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图片。相比而言,第一种方法是最好的,别看它代码最多,但它是完全用代码实现的,没有借助其他控件,也没有产生中间文件,用起来就好像网上的图片就是自己硬盘中的图片一样;第二种方法代码最简单,但它会产生硬盘文件,不过如果你恰巧需要下载文件的(就是说不但要显示,还想利用这个图片做其他操作),那么这个方法就是最佳选择了;第三种方法最复杂,需要用到网页控件,而且要用剪贴板过渡,不过如果你的软件本来就是需要用到网页控件的,或者你想获得同一网页的多张图片,那么这又是不二选择了。
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,但知道网页的URL,那么可以用WebBrowser控件显示网页,再从中找到需要的图片显示到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图片。相比而言,第一种方法是最好的,别看它代码最多,但它是完全用代码实现的,没有借助其他控件,也没有产生中间文件,用起来就好像网上的图片就是自己硬盘中的图片一样;第二种方法代码最简单,但它会产生硬盘文件,不过如果你恰巧需要下载文件的(就是说不但要显示,还想利用这个图片做其他操作),那么这个方法就是最佳选择了;第三种方法最复杂,需要用到网页控件,而且要用剪贴板过渡,不过如果你的软件本来就是需要用到网页控件的,或者你想获得同一网页的多张图片,那么这又是不二选择了。
追问
真的很感谢你的回答,回答好详细,但我还想知道一些:直接用鼠标把网页上的图片拖放到Picture1中,等于用鼠标复制的说法。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |