这个错误已经有很多帖子了,但我似乎找不到解决方案。当我使用 LoadPicture()
函数在用户窗体上的图像控件中编程加载图片时,会出现以下错误:
运行时错误 481 - 无效图片
手动加载时会出现类似的
无效图片
消息。
通过我的互联网搜索,我发现了许多关于这个错误的建议;大多数帖子倾向于是因为用户上传了一个 png 或其他无效图像(例如损坏的 jpg),一些人认为我的 temp
文件夹已满,还有一些人认为病毒是原因(我对最后一个持怀疑态度)。
在我的应用程序中,我的图片是
一个 jpg(在他们的
LoadPicture()
文章的备注部分中,MSDN 列出了可接受的格式)没有损坏(至少,我可以用 Windows 照片查看器/ Chrome/ Paint 正常查看它)
相对较小(即,它是 ~1MPx,我测试了一个 200MPx 的 jpeg,没有任何错误)
唯一有点不寻常的事情是,我正在使用以下代码直接从网络下载:
Public 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 'api to download files
Sub setPicTest()
Dim tmpImg As MSForms.Image 'create an image control on UserForm1 at runtime
Set tmpImg = UserForm1.Controls.Add("Forms.Image.1")
tmpImg.Picture = getImg("https://s-media-cache-ak0.pinimg.com/originals/22/e2/4d/22e24d3b5703a1c1cc43df5b13f53fd2.png")
End Sub
Function getImg(url As String) As IPictureDisp 'loads a picture from a url
Dim strFile As String 'file save location
strFile = Environ("Temp") & "\Temp.jpg" 'save *as jpeg* in %temp% folder
Debug.Print URLDownloadToFile(0, url, strFile, 0, 0) 'download file to temp folder
Set getImg = LoadPicture(strFile) 'load image -error-
Kill strFile 'clean up temp file
End Function
当我进行步骤时,一切都按预期运行。
- 我的用户窗体上出现了一个空的(没有图片)图像控件
- 在我的临时文件夹中出现了一个名为
temp.jpg
的文件- 这个文件似乎没有损坏
但是接下来代码执行时出现了错误。这特别令人惊讶,因为这段代码对于小缩略图图片一直都能正常工作,只有这些高分辨率的图片好像无法正常工作。