There's loads of posts on this error already but I can't seem to find a solution; when loading a picture programmatically (with the LoadPicture()
function) to an image control on a userform I get this error:
Run-time error 481 - Invalid picture
and a similar
Invalid picture
message when it's loaded manually.
From my internet searches I've found a whole load of advice on this error; most posts tend to be because the user is uploading a png or other invalid image (such as a corrupt jpg), some suggest that my temp
folder is full, others think a virus is the reason (I'm sceptical about that last one).
In my application, my picture is
A jpg (which MSDN lists as an acceptable format in the remarks section of their
LoadPicture()
article)Not corrupt (at least, I can view it with windows photoviewer/ Chrome/ Paint fine)
Relatively small (as in, it is ~1MPx and a 200MPx jpeg I tested loaded without any errors)
The only thing slightly unusual is that I'm downloading this straight from the web using the following code:
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
As I step through, everything runs as expected
- An empty (no picture) image control appears on my UserForm
- A file called
temp.jpg
appears in my temp folder- This file does not appear to be corrupted
But then code execution breaks on the error. This is particularly surprising as the code has been working fine for little thumbnail images, it's just these full resolution images don't seem to be working.