已知图片的URL地址,用代码怎样保存到磁盘
发布网友
发布时间:2022-04-29 11:27
我来回答
共2个回答
热心网友
时间:2022-06-26 22:28
我这是这么写的,应该很容易看懂吧
Private stream_Document As New ADODB.Stream
Public Sub SaveImageB(URL As String, SavePath As String)
Dim sFile
sFile = GetBody(URL) '取得图片的具休内容的过程
With stream_Document
.Type = 1 '以二进制模式打开
.Open
.write sFile '将字符串内容写入缓冲
.SaveToFile SavePath, 2 '将缓冲的内容写入文件
.Close
End With
End Sub
Private Function GetBody(URL)
' on error resume next
Dim Retrieval As New XMLHTTP30
'Set Retrieval = CreateObject("Microsoft.XMLHTTP")
With Retrieval
.Open "Get", URL, False, "", ""
.send
GetBody = .responseBody
End With
Set Retrieval = Nothing
End Function
vb里要先引用ADODB和XMLHTTP30,不懂可以问我
热心网友
时间:2022-06-26 22:29
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
Function DownloadFile(URL As String, LocalFilename As String) As Boolean
Dim lngRetVal As Long
lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then DownloadFile = True
End Function
使用示例:(最好判断一下扩展名)
URL="http://image.baidu.com/i?ct=503316480&z=3&tn=imagedetail&word=d&in=28523&cl=2&cm=1&sc=0&lm=-1&pn=15&rn=1&di=264528776&ln=2000"
DownloadFile URL,"C:\a.bmp"