这个错误已经有大量的帖子,但我似乎无法找到解决方案; (与LoadPicture()
功能)编程加载图片上的用户窗体的图像控制当出现此错误:加载JPEG图片的错误
Run-time error 481 - Invalid picture
和类似
时,它的手动加载Invalid picture
消息。
从我的网络搜索中,我发现了关于这个错误的一整套建议;大多数帖子往往是因为user is uploading a png或其他无效图像(such as a corrupt jpg),some suggest那我的temp
文件夹已满,others think病毒是原因(我对最后一个怀疑)。
在我的申请,我的照片是
JPG格式(在他们
LoadPicture()
文章的注释部分,其MSDN lists as an acceptable format)未损坏(至少,我可以与Windows photoviewer查看/ Chrome/Paint fine)
相对较小(因为它是〜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
当我步,一切运行预期
- 空(无图)图像控制出现在我的UserForm
- 一个名为
temp.jpg
的文件出现在我的临时文件夹中- 此文件不出现损坏
但随后的错误代码执行中断。这是特别令人惊讶的,因为代码对于小的缩略图图像一直工作正常,只是这些全分辨率图像似乎没有工作。
这是一个PNG文件。将它重命名为jpg将无济于事:) –