VB下载网页文件或图片的几种简易方法

前端之家收集整理的这篇文章主要介绍了VB下载网页文件或图片的几种简易方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

原贴链接:http://www.suntw.com/vb6/vb-xia-zhai-wang-ye-wen-jian-he-tu-pian.html

@H_301_2@一、使用 API 函数 URLDownloadToFile,下载网页文件(也可以是图片等其他文件)到硬盘

@H_301_2@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

@H_301_2@Dim nUrl As String,F As String,S As Long

@H_301_2@nUrl = "http://www.baidu.com/img/baidu_logo.gif": F = App.Path & "\My-1.bmp" '下载百度图片

@H_301_2@S = URLDownloadToFile(0,nUrl,F,0) '下载文件,0 表示成功

@H_301_2@nUrl = "http://www.baidu.com": F = App.Path & "\Baidu.html" '下载百度首页的网页

@H_301_2@S = URLDownloadToFile(0,0) '下载文件,0 表示成功

@H_301_2@二、使用 API 函数 OleLoadPicturePath,下载图片文件到变量,注意要勾选引用:OLE Automation

@H_301_2@Private Declare Function OleLoadPicturePath Lib "oleaut32.dll" (ByVal szURLorPath As Long,ByVal punkCaller As Long,ByVal clrReserved As OLE_COLOR,ByRef riid As TGUID,ByRef ppvRet As IPicture) As Long

@H_301_2@Private Type TGUID

@H_301_2@Data1 As Long: Data2 As Integer: Data3 As Integer: Data4(0 To 7) As Byte

@H_301_2@End Type

@H_301_2@Public Function LoadNetPicture(ByVal ImgSrc As String) As Picture

@H_301_2@Dim riid As TGUID

@H_301_2@riid.Data1 = &H7BF80980: riid.Data2 = &HBF32: riid.Data3 = &H101A

@H_301_2@riid.Data4(0) = &H8B: riid.Data4(1) = &HBB: riid.Data4(2) = &H0

@H_301_2@riid.Data4(3) = &HAA: riid.Data4(4) = &H0: riid.Data4(5) = &H30

@H_301_2@riid.Data4(6) = &HC: riid.Data4(7) = &HAB

@H_301_2@OleLoadPicturePath StrPtr(ImgSrc),0&,riid,LoadNetPicture

@H_301_2@End Function

@H_301_2@'使用方法:

@H_301_2@Dim ImgSrc As String,nPicture As Picture

@H_301_2@ImgSrc = "http://www.baidu.com/img/baidu_logo.gif"

@H_301_2@Set nPicture = LoadNetPicture(ImgSrc) '将图片下载到变量

@H_301_2@SavePicture nPicture,App.Path & "\MyImg.bmp" '保存到硬盘

@H_301_2@Picture1.Picture = nPicture

@H_301_2@'如果要将图片装载到控件,可以直接这样:

@H_301_2@Picture1.Picture = LoadNetPicture(ImgSrc)

@H_301_2@三、使用 Microsoft.XMLHTTP 对象,下载网页文件(也可以是图片等其他文件)到硬盘

@H_301_2@Private Sub DownNetFile(ByVal nUrl As String,ByVal nFile As String)

@H_301_2@Dim XmlHttp,B() As Byte

@H_301_2@Set XmlHttp = CreateObject("Microsoft.XMLHTTP")

@H_301_2@XmlHttp.Open "GET",False

@H_301_2@XmlHttp.Send

@H_301_2@If XmlHttp.ReadyState = 4 Then

@H_301_2@B() = XmlHttp.ResponseBody

@H_301_2@Open nFile For Binary As #1

@H_301_2@Put #1,B()

@H_301_2@Close #1

@H_301_2@End If

@H_301_2@Set XmlHttp = Nothing

@H_301_2@End Sub

@H_301_2@'使用方法:

@H_301_2@DownNetFile "http://www.baidu.com/img/baidu_logo.gif",App.Path & "\My-1.bmp" '下载百度图片

@H_301_2@DownNetFile "http://www.baidu.com",App.Path & "\Baidu.html" '下载百度首页的网页

@H_301_2@四、使用 WebBrowser 的 Document 对象,保存网页的所有图片

@H_301_2@'先用 WebBrowser1 显示一个网页,如:WebBrowser1.Navigate "http://www.baidu.com"

@H_301_2@'待网页加载完毕后,再执行以下语句:

@H_301_2@Dim nPath As String,K As Long,E,nRange

@H_301_2@nPath = App.Path & "\Tu\"

@H_301_2@WebBrowser1.Silent = True '关闭交互 禁止脚本错误

@H_301_2@For Each E In WebBrowser1.Document.All

@H_301_2@If E.tagName = "IMG" Then

@H_301_2@Set nRange = WebBrowser1.Document.body.createControlRange()

@H_301_2@nRange.Add E

@H_301_2@nRange.execCommand "Copy" '复制到剪贴板

@H_301_2@K = K + 1

@H_301_2@SavePicture Clipboard.GetData,nPath & K & ".bmp" '保存到硬盘

@H_301_2@End If

@H_301_2@Next

@H_301_2@五、用API调用系统的另存为下载对话框

@H_301_2@Private Declare Function DoFileDownload Lib "shdocvw.dll" (ByVal lpszFile As String) As Long

@H_301_2@'使用方法

@H_301_2@Dim nUrl As String

@H_301_2@nUrl = StrConv("http://www.baidu.com",vbUnicode)

@H_301_2@Call DoFileDownload(nUrl)

@H_301_2@六、用 WebBrowser1 的 ExecWB 方法调用网页另存为对话框:

@H_301_2@WebBrowser1.ExecWB OLECMDID_SAVEAS,OLECMDEXECOPT_DODEFAULT

@H_301_2@以上方法各有优缺点。

@H_301_2@Microsoft.XMLHTTP 对于大文件有利,支持断点续传,但需调用 getResponseHeader 进行数据检查。

@H_301_2@对于谷歌地图图片图片链接形如 http://mt1.google.cn/vt/lyrs=s@63&gl=cn&x=51694&s=&y=26884&z=16&s=Galile,仅第四种方法有效。

猜你在找的VB相关文章