By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,234 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,234 IT Pros & Developers. It's quick & easy.

GDI+ throws error when animated gif in picturebox

P: n/a
Ger
I have a form with a panel (Panel1) in which I dynamically create picturebox
controls. Works like a dream with static pictures, but not when the
picturebox.image is an animated gif. GDI+ throws the following error in this
case:

"An unhandled exception of type
'System.Runtime.InteropServices.ExternalException' occurred in
system.windows.forms.dll
Additional information: A generic error occurred in GDI+."

My code looks as follows:

Private Sub addImage(ByVal Im As Image)

'This sub will instantiate a new picturebox and create eventhandler
for it
'The image for the picturebox is passed as parameter to this sub

Dim p As New System.Windows.Forms.PictureBox
Try
p.Height = 44
p.Width = 80
p.Image = Im
p.BorderStyle = BorderStyle.None
p.SizeMode = PictureBoxSizeMode.AutoSize

Panel1.Controls.Add(p)
p.Invalidate()

AddHandler p.MouseDown, AddressOf Me.newToolboxIcon_MouseDown

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

What can cause the error? I tend to think of a bug in GDI+, because in some
other applications with one picturebox straight on the form (so not in a
panel, and not dynamically created) it works OK, also with animated gifs.

Please help!

/Ger

Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* "Ger" <ge*********@remove-this-part-of-address.planet.nl> scripsit:
I have a form with a panel (Panel1) in which I dynamically create picturebox
controls. Works like a dream with static pictures, but not when the
picturebox.image is an animated gif. GDI+ throws the following error in this
case:

"An unhandled exception of type
'System.Runtime.InteropServices.ExternalException' occurred in
system.windows.forms.dll
Additional information: A generic error occurred in GDI+."

My code looks as follows:

Private Sub addImage(ByVal Im As Image)

'This sub will instantiate a new picturebox and create eventhandler
for it
'The image for the picturebox is passed as parameter to this sub

Dim p As New System.Windows.Forms.PictureBox
Try
p.Height = 44
p.Width = 80
p.Image = Im
p.BorderStyle = BorderStyle.None
p.SizeMode = PictureBoxSizeMode.AutoSize

Panel1.Controls.Add(p)
p.Invalidate()

AddHandler p.MouseDown, AddressOf Me.newToolboxIcon_MouseDown

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

What can cause the error? I tend to think of a bug in GDI+, because in some
other applications with one picturebox straight on the form (so not in a
panel, and not dynamically created) it works OK, also with animated gifs.


Are you loading the image from a memorystream? Notice that all streams
must be kept alive as long as the animation is playing.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2

P: n/a
Ger
> >
What can cause the error? I tend to think of a bug in GDI+, because in some other applications with one picturebox straight on the form (so not in a
panel, and not dynamically created) it works OK, also with animated
gifs.
Are you loading the image from a memorystream? Notice that all streams
must be kept alive as long as the animation is playing.


Thank you very much for your reply Herfried,

you are right, I am loading the image from a database row with an
OleDbDataReader as follows:

Dim myCommand As New OleDbCommand("Select * from Icons where
IconCategory =" & Category.ToString & " AND IconIsMain = True", conn)
Dim myReader As OleDbDataReader
Dim bufferSize As Integer = 3200
Dim ByteBlob(3200 - 1) As Byte
Dim retval As Long
Dim Strm As New MemoryStream(ByteBlob)
Dim Im As Image

retval = myReader.GetBytes(ImageColOrdinal, 0, ByteBlob, 0,
bufferSize)
If retval > 1 Then
Im = Image.FromStream(Strm)
addImage(Im) 'store image in picturebox
End If

But how do I keep the stream "alive" as you suggest??

/Ger


Nov 20 '05 #3

P: n/a
* "Ger" <ge*********@remove-this-part-of-address.planet.nl> scripsit:
Are you loading the image from a memorystream? Notice that all streams
must be kept alive as long as the animation is playing.


Thank you very much for your reply Herfried,

you are right, I am loading the image from a database row with an
OleDbDataReader as follows:

Dim myCommand As New OleDbCommand("Select * from Icons where
IconCategory =" & Category.ToString & " AND IconIsMain = True", conn)
Dim myReader As OleDbDataReader
Dim bufferSize As Integer = 3200
Dim ByteBlob(3200 - 1) As Byte
Dim retval As Long
Dim Strm As New MemoryStream(ByteBlob)
Dim Im As Image

retval = myReader.GetBytes(ImageColOrdinal, 0, ByteBlob, 0,
bufferSize)
If retval > 1 Then
Im = Image.FromStream(Strm)
addImage(Im) 'store image in picturebox
End If

But how do I keep the stream "alive" as you suggest??


I am not sure if the stream isn't closed automatically after execution
of this piece of code...

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.