dchapma1
asked on
Cannot trap Runtime Error 481 'Invalid Picture'
I have an application with a Picture box that I use to display an image to the user. The images are JPG files. Recently the user told me that occasionally he gets a corrupt jpg filr from his card reader and the application displays the Runtime Error 481, 'Invalid Picture' then drops to the desktop.
I added the following Error Handling the the application:
On error GoTo ErrorHandler
Picturebox1.Picture = LoadPicture(Filename)
Picture1.Visible = True
Exit SUb
ErrorHandler:
In this spot I have code to display an error message using the VB err object,, things like err.description and the like.
Resume Next
The problem is that VB NEVER traps the error, even if I try to load a known bad jpg file the application still generates the Runtime error and crashes. Running it in the VB IDE it generates the runtime error and when I break the line that is trying to load the picture is highlighted.
So why can't I trap this error? I need to trap this error and if the err.number = 481 have the error handler load an alternate image into the picture box and resume.
Thanks much
Don
I added the following Error Handling the the application:
On error GoTo ErrorHandler
Picturebox1.Picture = LoadPicture(Filename)
Picture1.Visible = True
Exit SUb
ErrorHandler:
In this spot I have code to display an error message using the VB err object,, things like err.description and the like.
Resume Next
The problem is that VB NEVER traps the error, even if I try to load a known bad jpg file the application still generates the Runtime error and crashes. Running it in the VB IDE it generates the runtime error and when I break the line that is trying to load the picture is highlighted.
So why can't I trap this error? I need to trap this error and if the err.number = 481 have the error handler load an alternate image into the picture box and resume.
Thanks much
Don
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Well,
I tried both methoids this morning. It still generates the error and will not go past the Picture1.Picture = LoadPicture(Filename)
If the jpg file is not corrupt this whole routine works very well.
I can post the entire subrouting tonight after work if you think that might help..
Don
I tried both methoids this morning. It still generates the error and will not go past the Picture1.Picture = LoadPicture(Filename)
If the jpg file is not corrupt this whole routine works very well.
I can post the entire subrouting tonight after work if you think that might help..
Don
Ok, I'll would to check it. I corrupted a lot of jpgs here, and I can't reproduce your problem :(
ASKER
Here is the complete Sub on this problem
Private Sub LoadThumbnails()
lblSearching.Visible = False
'Array containing Image Path and Filenames
Filename0 = Images(0, 0) & Images(0, 1)
Filename1 = Images(1, 0) & Images(1, 1)
FileName2 = Images(2, 0) & Images(2, 1)
On Error GoTo ErrorThumb0
Thumb(0).Picture = LoadPicture(Filename0)
If err.Number <> 0 Then
GoTo ErrorThumb0
End If
Thumb(0).Visible = True
Thumb(0).Refresh
lblID0.Visible = True
lblID0.Refresh
HighlightSquare0.Visible = False
On Error GoTo ErrorThumb1
Thumb(1).Picture = LoadPicture(Filename1)
If err.Number <> 0 Then
GoTo ErrorThumb0
End If
Thumb(1).Visible = True
Thumb(1).Refresh
lblID1.Visible = True
lblID1.Refresh
On Error GoTo ErrorThumb2
Thumb(2).Picture = LoadPicture(FileName2)
Thumb(2).Visible = True
Thumb(2).Refresh
lblID2.Visible = True
lblID2.Refresh
HighlightSquare0.Visible = True
On Error GoTo ErrorLarge
LargeImage.Picture = LoadPicture(Filename0)
LargeImage.Visible = True
lblCurrent.Visible = True
cmdScrollNext.Visible = True
cmdScrollPrevious.Visible = True
Exit Sub
'--------------------Error Handlers------------------ ------
ErrorThumb0:
Load frmDialog
frmDialog.txtMessage.Text = Str$(err.Number) & " Loading Thumbnail 1." & vbCrLf _
& err.Description & vbCrLf & Filename0 & " is not able to open."
Thumb(0).Picture = LoadPicture ' placeholder for Image not available.jpg
Thumb(0).Visible = True
Thumb(0).Refresh
lblID0.Visible = True
lblID0.Refresh
Resume Next
ErrorThumb1:
Load frmDialog
frmDialog.txtMessage.Text = Str$(err.Number) & " Loading Thumbnail 2." & vbCrLf _
& err.Description & vbCrLf & Filename1 & " is not able to open."
Thumb(1).Picture = LoadPicture ' placeholder for Image not available.jpg
Thumb(1).Visible = True
Thumb(1).Refresh
lblID1.Visible = True
lblID1.Refresh
Resume Next
ErrorThumb2:
Load frmDialog
frmDialog.txtMessage.Text = Str$(err.Number) & " Loading Thumbnail 3." & vbCrLf _
& err.Description & vbCrLf & FileName2 & " is not able to open."
Thumb(2).Picture = LoadPicture ' placeholder for Image not available.jpg
Thumb(2).Visible = True
Thumb(2).Refresh
lblID2.Visible = True
lblID2.Refresh
Resume Next
ErrorLarge:
Load frmDialog
frmDialog.txtMessage.Text = Str$(err.Number) & " Loading Thumbnails." & vbCrLf _
& err.Description & vbCrLf & Filename0 & " is not able to open."
LargeImage.Picture = LoadPicture ' placeholder for Image not available.jpg
LargeImage.Visible = True
lblCurrent.Visible = True
Resume Next
End Sub
LIke I indicated, if the JPG file is not corrupted everything works fine...
Don
Private Sub LoadThumbnails()
lblSearching.Visible = False
'Array containing Image Path and Filenames
Filename0 = Images(0, 0) & Images(0, 1)
Filename1 = Images(1, 0) & Images(1, 1)
FileName2 = Images(2, 0) & Images(2, 1)
On Error GoTo ErrorThumb0
Thumb(0).Picture = LoadPicture(Filename0)
If err.Number <> 0 Then
GoTo ErrorThumb0
End If
Thumb(0).Visible = True
Thumb(0).Refresh
lblID0.Visible = True
lblID0.Refresh
HighlightSquare0.Visible = False
On Error GoTo ErrorThumb1
Thumb(1).Picture = LoadPicture(Filename1)
If err.Number <> 0 Then
GoTo ErrorThumb0
End If
Thumb(1).Visible = True
Thumb(1).Refresh
lblID1.Visible = True
lblID1.Refresh
On Error GoTo ErrorThumb2
Thumb(2).Picture = LoadPicture(FileName2)
Thumb(2).Visible = True
Thumb(2).Refresh
lblID2.Visible = True
lblID2.Refresh
HighlightSquare0.Visible = True
On Error GoTo ErrorLarge
LargeImage.Picture = LoadPicture(Filename0)
LargeImage.Visible = True
lblCurrent.Visible = True
cmdScrollNext.Visible = True
cmdScrollPrevious.Visible = True
Exit Sub
'--------------------Error
ErrorThumb0:
Load frmDialog
frmDialog.txtMessage.Text = Str$(err.Number) & " Loading Thumbnail 1." & vbCrLf _
& err.Description & vbCrLf & Filename0 & " is not able to open."
Thumb(0).Picture = LoadPicture ' placeholder for Image not available.jpg
Thumb(0).Visible = True
Thumb(0).Refresh
lblID0.Visible = True
lblID0.Refresh
Resume Next
ErrorThumb1:
Load frmDialog
frmDialog.txtMessage.Text = Str$(err.Number) & " Loading Thumbnail 2." & vbCrLf _
& err.Description & vbCrLf & Filename1 & " is not able to open."
Thumb(1).Picture = LoadPicture ' placeholder for Image not available.jpg
Thumb(1).Visible = True
Thumb(1).Refresh
lblID1.Visible = True
lblID1.Refresh
Resume Next
ErrorThumb2:
Load frmDialog
frmDialog.txtMessage.Text = Str$(err.Number) & " Loading Thumbnail 3." & vbCrLf _
& err.Description & vbCrLf & FileName2 & " is not able to open."
Thumb(2).Picture = LoadPicture ' placeholder for Image not available.jpg
Thumb(2).Visible = True
Thumb(2).Refresh
lblID2.Visible = True
lblID2.Refresh
Resume Next
ErrorLarge:
Load frmDialog
frmDialog.txtMessage.Text = Str$(err.Number) & " Loading Thumbnails." & vbCrLf _
& err.Description & vbCrLf & Filename0 & " is not able to open."
LargeImage.Picture = LoadPicture ' placeholder for Image not available.jpg
LargeImage.Visible = True
lblCurrent.Visible = True
Resume Next
End Sub
LIke I indicated, if the JPG file is not corrupted everything works fine...
Don
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
To be fair, I split the points.. Even though I found my own solution, I appreciate it when someone takes the time to test code and offer alternative solutions.
Thanks
Thanks
Well, that points have like a bitter taste on them, sorry i couldn't help you more.
Anyway, what did you do to fix your problem.
Anyway, what did you do to fix your problem.
ASKER
I simply configured VB NOT to Break on all errors. Instead, I checked the option,Break on all Unhandled errors.
Since I had error handling code in place, it used my error handler instead of breaking on the error.
Worked like a charm.
Now I will add a custom "Image Not Available" jpg file in a folder, and in the error handler I will do a LoadPicture (ImageNot available.jpg) and resume next. That way, the user's GUI will have a visual indication of the error and in addition I will post an event to the Application event log indicating the error.
Don
Since I had error handling code in place, it used my error handler instead of breaking on the error.
Worked like a charm.
Now I will add a custom "Image Not Available" jpg file in a folder, and in the error handler I will do a LoadPicture (ImageNot available.jpg) and resume next. That way, the user's GUI will have a visual indication of the error and in addition I will post an event to the Application event log indicating the error.
Don
Nice to know. I never thought on that.
Private Sub Form_Load()
On Error GoTo erdfd
Dim c As StdPicture
Set c = LoadPicture("c:\image1.jpg
Picture1.Picture = c
fgfghg:
'Rest of code
Exit Sub
erdfd:
MsgBox Err.Number
GoTo fgfghg
End Sub
Sorry for the horrible named lines, i'm half asleep now...