Private Sub Command1_Click()
Dim http As WinHttp.WinHttpRequest
Dim imageUrl As String
Dim imageFilePath As String
Dim byteArray() As Byte
Dim fileNumber As Integer
' 要下载的图片URL
imageUrl = "https://s2.best-wallpaper.net/wallpaper/1920x1080/1603/Beautiful-universe-stars-galaxies-purple-color-nebula_1920x1080.jpg" ' 替换成要下载的图片URL
' 保存图片的本地路径
imageFilePath = "a.jpg" ' 替换成希望保存的路径
' 创建WinHttpRequest对象
Set http = New WinHttp.WinHttpRequest
' 发送GET请求
On Error GoTo ErrorHandler
http.Open "GET", imageUrl, False
http.Send
' 检查请求是否成功
If http.Status = 200 Then
' 获取响应体
byteArray = http.ResponseBody
' 打开文件并写入字节数据
fileNumber = FreeFile
Open imageFilePath For Binary Access Write As #fileNumber
Put #fileNumber, 1, byteArray
Close #fileNumber
MsgBox "图片下载成功!"
Else
MsgBox "无法下载图片,状态码:" & http.Status
End If
Exit Sub
ErrorHandler:
MsgBox “出现错误:” & Err.Description
If fileNumber > 0 Then Close #fileNumber
End Sub