Astrify Astrify - 2 months ago 18
Vb.net Question

GDI+ generic error - cannot see why, everything looks fine

Im grabbing data from a database, an item image (base 64 string), and trying to display it on my page. This is what I've got:

Dim data() As Byte = CType(dsData.Tables(0).Rows(0).Item("itemimage"), Byte())
Dim stre As New MemoryStream(data, 0, data.Length)
stre.Write(data, 0, data.Length)
stre.Flush()
Dim bmp As Image = Image.FromStream(stre, True, False)
If Not bmp Is Nothing Then
bmp.Save("temp_stock.png", Imaging.ImageFormat.Png)
imgItem.ImageUrl = "temp_stock.png"
End If

stre.Close()
stre.Dispose()


I cannot see this issue, all users on the system have write access to the folder, and it still comes up with generic GDI+ error when i do
bmp.Save()


Any advice?

Answer

I'm assuming your database data is varbinary, not a Base64 image stored as a string. If that's the case you can directly convert a Byte Array to a base64 image.

Dim data() As Byte = CType(dsData.Tables(0).Rows(0).Item("itemimage"), Byte())
imgItem.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(data);

By the way you are getting an error because you are flushing the stream before using it to save the bitmap. Flushing clears all the content from the stream.