|
-
Mar 31st, 2010, 03:19 PM
#1
[RESOLVED] Robust response to Out Of Memory Exception
I've noticed that GDI+ and other graphics can crash when asked to deal with very large images, and the crash doesn't always occur where you'd expect it. I am getting this when testing the slide show program I posted to the code bank yesterday. This is the code concerned:
vb.net Code:
Private Sub animTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles animTimer.Tick If infos.Count = 0 Then Exit Sub ticks = (ticks + 1) Mod 200 Select Case ticks Case 0 'phase 1: Pic2 is shown, Pic1 is hidden. Start loading pic1. picIndex = (picIndex + 1) Mod infos.Count Pic1.Load(infos(picIndex).FullName) Case 50 To 99 'phase 2: Start fading pic2 Try PicForm2.Opacity -= 0.02 Catch Ex As Exception MessageBox.Show(Ex.Message) End Try If ticks = 65 Then Me.Text = "Easy Slide Show: " & infos(picIndex).Name Case 100 'phase 3: Pic1 is shown. Start loading Pic2 picIndex = (picIndex + 1) Mod infos.Count Pic2.Load(infos(picIndex).FullName) Case 150 To 199 'phase 4: Start Unfading Pic2 PicForm2.Opacity += 0.02 If ticks = 165 Then Me.Text = "Easy Slide Show: " & infos(picIndex).Name End Select End Sub
The crash only occurs when asked to deal with a 48Mpixel (6000 x 8000) image, and then only when running in the debugger. The picture box loads the image successfully and displays it OK. The Out of Memory exception is not thrown until I try to fade the form by changing its opacity (line 12).
Unfortunately Try-Catch doesn't work in this case. If it did I could simply skip the image concerned or maybe show it at a lower resolution. But the OOM error is thrown before the exception is caught. The same applies whether I put Try-Catch just around the offending line as above or round the whole loop.
I realize that a 48 Megapixel image is a lot to ask, certainly in limited memory, and in normal running even that does not fail. But where should I draw the line? Is the only way to set an arbitrary upper size limit to the images to ward off a potential crash? In this application I would prefer just to skip the image if need be. Can someone recommend a robust way to deal with the exception?
BB
Last edited by boops boops; Mar 31st, 2010 at 03:23 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|