Unhandled Exception in GDI+ Part III

G

Guest

As described in parts I and II of the subject thread I have a program that
displays images whose source is not under my control. Current batch of test
images contains an animated gif that is not well behaved. We see the first
frame and eventually it causes the subject exception. (IE 7 and Windows Mail
display it just fine and I ask in Part I how to CYA. In Part II I ask about
problems with displaying the error message)

Once I have returned from error processing, I get two big ugly X's on the
program window. One is in a bindingnavigator, though the text displays fine.
The other is over the main picturebox. What is interesting is that the
datagridview which is displaying thumbnails works just fine. (I suspect
because there is a new imagecell for each thumbnail.)

QUESTION: How do I recover from the GDI+ error? How do I goose the relevant
controls so they behave themselves. The main picture box is refreshed
everytime a new image is displayed.
--
Regards,
Al Christoph
Senior Consultant
Three Bears Software, LLC
just right software @ just right prices @ 3bears.biz
Microsoft Certified Partner (ISV)
Coming soon: Windows Mail for Vista.
 
M

Michael C

Al Christoph said:
QUESTION: How do I recover from the GDI+ error? How do I goose the
relevant
controls so they behave themselves. The main picture box is refreshed
everytime a new image is displayed.

From my previous post, if you do the animation yourself you will have better
control over error handling.

Michael
 
G

Guest

That is true. However, while i didn't specifically state it, there is a more
general problem here. While I don't have an example at hand, I have observed
that corrumpt images of any kind bring the image display to its knees.

MS LURKERS TAKE NOTE: This is not nice. GDI+ should recover itself in a much
friendlier way. The PictureBox control should have some way of telling the
programmer that it's been messed up. Other controls on the screen should NOT
be messed up at all. Given the length of time that GDI+ has been around and
the fundamental nature of it, we have yet another reason to say that xxx is
not ready for prime time.

So the QUESTION REMAINS - how do you recover when anything has caused a GDI+
error?

--
Regards,
Al Christoph
Senior Consultant
Three Bears Software, LLC
just right software @ just right prices @ 3bears.biz
Microsoft Certified Partner (ISV)
Coming soon: Windows Mail for Vista.
 
L

Linda Liu [MSFT]

Hi Al,

I suggest that you embrace the code for creating image in a try...catch
block. If an exception occurs when you create an image object, don't
display it in the picture box.

For further discussion, could you please send your sample project that
could just reproduce the problem to me? To get my actual email address,
remove 'online' from my displayed email address.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Your suggestion of the try catch block is a sound one. Unfortunately I
already do it, and the suggesion is probably not applicable in this case. Let
me see if I can convince you:)

In psuedo code i have
try
load picture box image with animated gif
catch
any problems
end try
next operation

The hypothetical times sequece based on my observation is:
1. My program loads picture box image with animated gif.

2. GDI+ displays first image AOK and returns to program setting up a timer
and as near as I can tell NOT checking for the validity of the rest of the
gif. (Shame, shame on the programmer who thought speed was more important
than reliability. Probably the same one that coded the bug that allowed for
infected GDI's:)))

3. Program does "next operation".

4. Program becomes idle.

5. EXTERNAL to the program, GDI+ animation timer expires and perhaps the
next frame is loaded successfully or perhaps GDI+ throws an external
exception messing up the picture box in the process.

The external exception is a bear to catch. You have to use
appDomain.currentdomain.unhandledexception or some such thing. And even then
as documented in part II of this series the results are NOT pleasant.

Unfortunately my code is not readily sharable in the current incarnation. I
will try to reproduce the problem in a much smaller and simpler program.
Should not be too hard. Once done, I''ll forwrd the program and the image to
you per your instructions.

--
Regards,
Al Christoph
Senior Consultant
Three Bears Software, LLC
just right software @ just right prices @ 3bears.biz
Microsoft Certified Partner (ISV)
Coming soon: Windows Mail for Vista.
 
M

Michael C

Al Christoph said:
That is true. However, while i didn't specifically state it, there is a
more
general problem here. While I don't have an example at hand, I have
observed
that corrumpt images of any kind bring the image display to its knees.

MS LURKERS TAKE NOTE: This is not nice. GDI+ should recover itself in a
much
friendlier way. The PictureBox control should have some way of telling the
programmer that it's been messed up. Other controls on the screen should
NOT
be messed up at all. Given the length of time that GDI+ has been around
and
the fundamental nature of it, we have yet another reason to say that xxx
is
not ready for prime time.

So the QUESTION REMAINS - how do you recover when anything has caused a
GDI+
error?

I've written a fairly complex image processing app over the last 4 years
(not full time) and am yet to use the picturebox for anything except
displaying static icons and have not encountered the problems you mentioned.
Perhaps you should dump the picturebox also? Every programming language has
limitations and quite often it is up to you to work around them.

Michael
 
G

Guest

Yep. Getting around limitations is how I've been making a livin' for 40 years
at this game. Even created my own computing center in the '70's to get
around campus limitations:) Ah memories.

If I try something different it would be to abandon VS altogether and
rewrite my existing Delphi application. OTH I didn't pay $1500 per year for
the privilege of using Delphi. (You don't want to see the length of the punch
list I have. It isn't pleasant. MS wants us to develop applications for Vista
but its own people can't answer my questions - not the issue here BTW - about
my particular piece of the puzzle Try that on for starters. I've forgivn
them for that sort of because as a result I will have a revolutionary app not
just a ho hum one.)

So forgive me if I get a bit cheeky.

--
Regards,
Al Christoph
Senior Consultant
Three Bears Software, LLC
just right software @ just right prices @ 3bears.biz
Microsoft Certified Partner (ISV)
Coming soon: Windows Mail for Vista.
 
M

Michael C

Al Christoph said:
If I try something different it would be to abandon VS altogether and
rewrite my existing Delphi application. OTH I didn't pay $1500 per year
for
the privilege of using Delphi.

This seems like a huge over reaction, changing to delphi over 1 little
picturebox issue. If you leave another control to run in another thread then
you won't be able to catch exceptions of course. Writing something yourself
seems like a pretty standard solution to me and should give you the control
you need. I don't think it would be that difficult a task as you can use the
bitmap object to get each frame and then it's only a matter of discovering
the timing required.

Michael
 
L

Linda Liu [MSFT]

Hi Al,

After doing some reading and tests, I found a workaround to solve this
problem. That is to check the validity of an animated GIF image before
showing it on a PictureBox control. If it is valid, show it directly on the
picture box; otherwise create a new new JPG image which contains the first
valid frame in the original damaged GIF image.

To valid an animated GIF image is damaged or not, we navigate to each frame
in the image. If no exception is thrown, the image is valid; otherwise, it
is damaged.

The following is a code snippet to check if an animated GIF image is valid
or not, and if not, create a new JPG image.

Imports System.Drawing.Imaging
Imports System.IO

Private Sub CheckandRestoreGIF(ByRef img As Image)

Dim badframeIndexes As New List(Of Integer)
Dim count As Integer = img.GetFrameCount(FrameDimension.Time)
For i As Integer = 0 To count - 1
Try
img.SelectActiveFrame(FrameDimension.Time, i)
Catch ex As Exception
badframeIndexes.Add(i)
End Try
Next
If (badframeIndexes.Count > 0) Then
Dim indexes As String = ""
For i As Integer = 0 To badframeIndexes.Count - 1
indexes += badframeIndexes(i).ToString() & ","
Next
indexes = indexes.Substring(0, indexes.Length - 1)
MsgBox("Frames No." & indexes.ToString() & " are damaged!")
End If

If (badframeIndexes.Count > 0 And badframeIndexes.Count < count)
Then
Dim stream As New MemoryStream
Dim encoderpara As New EncoderParameters(1)

For i As Integer = 0 To count - 1
If Not badframeIndexes.Contains(i) Then
img.SelectActiveFrame(FrameDimension.Time, i)
img.Save(stream, ImageFormat.Bmp)
Exit For
End If
Next

Dim newimg = Image.FromStream(stream)
img.Dispose()
img = newimg
stream.Close()
stream.Dispose()

ElseIf (badframeIndexes.Count = count) Then
img.Dispose()
img = Nothing
End If
End Sub

I also enclose the revised sample project. Please run it on your machine to
see if it is what you want.

I look forward to your reply.


Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top