Unhandled Exception in GDI+

G

Guest

I am getting the subject error message when an animated gif is displayed in a
picture box. I did not create the gif and in general what will be displayed
in that box is NOT under my control. (Other animated gifs jpegs etc display
fine.)

The ugly thing is that other Vista programs, namely Windows Mail and IE7,
display the gif just fine.

Clearly they are doing something to protect themselves that I'm not doing. I
just:

If picturebox.image isnot nothing then
picturebox.image.dispose
picturebox.image = nothing
end if
dim newImage as image = loadImageThroughAllTheMagicOfMyProgram
picturebox.image = newImage

QUESTION: What do I do to check and tweak newImage to make sure that it
isn't somehow slightly broken?

QUESTION: Is the dispose code necessary? The system should not dispose the
picturebox.image because it doesn't know what we want to do with it. If
someone knows that if the reference count on an image not otherwise stored
runs to zero the image is disposed, then I'd be glad to hear of it.

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: What do I do to check and tweak newImage to make sure that it
isn't somehow slightly broken?

Not sure. You could try displaying each frame of the image yourself. That is
a lot more complicated but will give you a better idea of what the error is
and how to avoid it.
QUESTION: Is the dispose code necessary? The system should not dispose the
picturebox.image because it doesn't know what we want to do with it. If
someone knows that if the reference count on an image not otherwise stored
runs to zero the image is disposed, then I'd be glad to hear of it.

Dispose code is necessary if you want the object removed from memory
immediately. This is a good idea.

Michael
 
B

Bob Powell [MVP]

I think i'd be inclined to remove the image from the picturebox before
disposing of it.

You should have a reference to the image somewhere else other than the pb.
If so, dispose of that. if not do:

if picturebox.image isnot nothing then
dim i as image=picturebox.image
picturebox.image=null
i.dispose
end if

otherwise an animation might come along and throw an exception for an object
not set to an instance or some such error.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
L

Linda Liu [MSFT]

Hi Al,

You have mentioned that the animated gif is not created by the application
and you haven't the control over what will be displayed in the picture box.

Where is the animated gif created? Could you please explain more on "you
haven't the control of what will be displayed in the picture box"? I
noticed that you set the Image property of the PictureBox in your program,
which means you're contolling what is displayed in the picture box.
What do I do to check and tweak newImage to make sure that it
isn't somehow slightly broken?

When we create a new image object and there are some errors during the
image's creating, an exception will occur. We could embrace the code for
creating the new image object in a try..catch block to catch the exception.
The following is a sample.

Try
dim newImage as image = loadImageThroughAllTheMagicOfMyProgram
Catch ex As ArgumentException
MessageBox.Show("There was an error. ")
End Try
Is the dispose code necessary?

The Dispose method releases all resources used by this image. I think it is
a good idea.

Hope this helps.
If you have anything unclear, please feel free to let me know.


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

My apologies for using confusing language. In attempting to not describe my
project in too much detail I have been unclear.

I create the image in the technical sense of
dim bitmap as new bitmap (strean)
picturebox.image = bitmap

The stream is the result of several transformations of the original data
which is encoded. This process is highly reliable to the tune of no detected
errors in 5000 images. So it would surprise me if it is a problem in this
case.

The original data is produced by other random folk from around the world who
may or may not be competent to manipulate image files.

Thus, I have no control over the quality of the creation of the original
data that feeds the process. Essentially other folk create files which I'm
trying to read and display and I have no control over the contents of the
files. (In previous incarnations of this program I even stood on my head to
make sure that the extension matched the file format. So far that has not
been an issue. GDI+ is figuring out what to do with the bits in stream that I
feed it and hence the contributor's notion of the file format as expressed in
the extention is irrelevant. When I get around to exporting the images I'll
probably worry about this.)
--
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.
 

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