Convert an Image/Bitmap Object to Byte Array without specifying a format?
Posted on 2005-04-05
How can I convert an Image or Bitmap object to a byte array without specifying an image format? I want to take a bitmap object and save it to sql server preserving its existing format. Every sample I've seen includes specifying the format when you call Image.Save, and I dont want it changing the fileformat on me.
I've tried routines that use the ImageConverter object and have also try saving to a MemoryStreak and then reading it out to a byte array. So far all methods I've tried result in a "General GDI+ Error".
Here's my latest code that throws an error on line 2.
Dim ms As New MemoryStream
Dim buffer(ms.Length - 1) As Byte
ms.Read(buffer, 0, ms.Length)
However, I've noticed in debug mode, after the error is thrown, I can reinitialzie my memorystream, run it again, and it works fine. So it only throws the error every other time.