我只是说,因为我实际上不需要存储我不喜欢的文件,除非我必须这样做。
我讨厌放弃!虽然我仍然觉得(正如我在上面的评论中提到的)将文件保存到用户的临时目录是一种简单易行的方法。事实上,我会为你提到这两种方法。
要测试此示例,请在 Excel 中创建一个用户窗体。接下来,执行此操作。
- 在其中放置一个 TextBox、Image 和一个 Commandbutton 控件。
- 接下来添加inet控件。为此,您必须通过其他控件并设置对
Microsoft Internet Transfer Control
您的用户表单的引用,如下所示。
接下来运行用户表单,然后将图像的 URL 粘贴到文本框中。我正在测试它http://static.freepik.com/free-photo/thumbs-up-smiley_17-1218174614.jpg
当您单击命令按钮时,图像将填充到图像控件中。
逻辑:
代码所做的是使用 inet 控件检索 URL 中的图像,然后将其存储在byte
数组中(而不是您请求的目录)。然后我获取该字节数组并将其转换为内存中的图像,然后将其分配给图像控件。
用户表单代码
Option Explicit
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Private Declare Function CreateStreamOnHGlobal Lib "ole32.dll" _
(ByRef hGlobal As Any, ByVal fDeleteOnResume As Long, ByRef ppstr As Any) As Long
Private Declare Function OleLoadPicture Lib "olepro32.dll" _
(ByVal lpStream As IUnknown, ByVal lSize As Long, ByVal fRunMode As Long, ByRef riid As GUID, ByRef lplpObj As Any) As Long
Private Declare Function CLSIDFromString Lib "ole32.dll" _
(ByVal lpsz As Long, ByRef pclsid As GUID) As Long
Private Const SIPICTURE As String = "{7BF80980-BF32-101A-8BBB-00AA00300CAB}"
Dim boolSuccess As Boolean
Private Sub CommandButton1_Click()
Dim URL As String
Dim bytes() As Byte
Dim ipic As IPictureDisp
URL = TextBox1.Text
'~~> Store the image from the url in a bytes array
bytes() = Inet1.OpenURL(URL, icByteArray)
'~~> Convert Byte Array into Image
Set ipic = ImageFromByteAr(bytes)
Image1.PictureSizeMode = fmPictureSizeModeStretch
If boolSuccess = True Then
'~~> Load Picture
Image1.Picture = ipic
Else
MsgBox "Unable to convert to picture"
End If
End Sub
Public Function ImageFromByteAr(ByRef byt() As Byte) As IPicture
On Error GoTo Whoa
Dim ippstr As IUnknown
Dim tGuid As GUID
If Not CreateStreamOnHGlobal(byt(LBound(byt)), False, ippstr) Then
CLSIDFromString StrPtr(SIPICTURE), tGuid
OleLoadPicture ippstr, UBound(byt) - LBound(byt) + 1, False, tGuid, ImageFromByteAr
End If
Set ippstr = Nothing
boolSuccess = True
Exit Function
Whoa:
boolSuccess = False
End Function
这是方法2(最简单的方法)
将文件保存到用户的临时目录
Option Explicit
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Const MAX_PATH As Long = 260
Function TempPath() As String
TempPath = String$(MAX_PATH, Chr$(0))
GetTempPath MAX_PATH, TempPath
TempPath = Replace(TempPath, Chr$(0), "")
End Function
Public Sub Test()
'
'~~> Rest of your code
'
FileNum = FreeFile
Open TempPath & "\map.JPG" For Binary Access Write As #FileNum
'
'~~> Rest of your code
'
End Sub
Sub InsertPic()
'
'~~> Rest of your code
'
Dim pic As String
Dim myPicture As Picture
pic = TempPath & "\map.JPG"
Set myPicture = ActiveSheet.Pictures.Insert(pic)
'
'~~> Rest of your code
'
End Sub