這個錯誤已經有大量的帖子,但我似乎無法找到解決方案; (與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將無濟於事:) –