GDI+ throws error when animated gif in picturebox

G

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
 
H

Herfried K. Wagner [MVP]

* "Ger said:
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.
 
G

Ger

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
 
H

Herfried K. Wagner [MVP]

* "Ger said:
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...
 

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