A generic error occurred in GDI+ when converting a picture to png in a stream:

M

Martin Widmer

Hi Folks

In my object I am trying to implement a function in order to render the
picture to XML (to generate RDL for SQL Server). That Function causes the
error mentioned in the subject at the marked line. I read various articles
about it but none of them seems to apply as I am not writing to a file or to
a response in ASP, instead it's a pure VB class that is used from a winforms
applciation. I am using VS 2005.

Private Function PictureToBase64(ByVal oPicture As Bitmap) As String
'Convert a bitmap to base64 encoded string for storage in RDL or XML
Dim sBase64 As String
Dim oMemory As New System.IO.MemoryStream()

'Save picture to stream'Convert stream to base64 encoded string
sBase64 = System.Convert.ToBase64String(oMemory.ToArray)
'Close stream and clean up
oMemory.Close()
oMemory = Nothing

Return sBase64
End Function

Thanks for suggestions

Martin
 
M

Martin Widmer

I found the problem: I was reading the image from the database using a
memorystream object to convert it from an array of bytes to a bitmap. The
memory stream object then was destroyed and hence the GDI+ specialty that
the creating stream must live as long as the bitmap was violated. --> That's
why I got the GDI+ error when trying to convert the same bitmap again later
to Base64.

Solution: use an object variable for the memorystream to ensure it lives as
long as the bitmap object itself, actually I am recycling it to convert the
image back to Base64

Let's see if the converted image is still good :)

Martin
 

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