tags:

views:

3251

answers:

1

Hello everyone,

I have a JPEG "image" (actually a BLOB in a database) which I want to import/convert into a "Bitmap" structure in memory. The reason is that I use a third party library which is unable to work with JPEG images and I need to pass an uncompressed bitmap (as a pointer). All I found so far are ways to convert between different formats on disk but saving the image as bitmap first and re-import it will take far too long.

I don't know much about .NET but I think a System.Drawing.Bitmap should be able to hold the uncompressed data. I'm working with C# and Visual Studio 2008.

+8  A: 
// blob is a byte[] retrieved from DB
Bitmap bmp = new Bitmap(new MemoryStream(blob));
Mehrdad Afshari
Just as a word of warning - *don't* close the stream yourself! (Not that the answer did, but it's an obvious thing to try.) After constructing the Bitmap, it "owns" the stream. Disposing of the bitmap will close the stream. It will get upset if you close the stream first.
Jon Skeet
I updated the answer to avoid the pitfall that Jon mentioned.
Mehrdad Afshari
When I try this on Windows Mobile 6.1, I get an ArgumentException which says the parameter is invalid. I know that the array in the MemoryStream does contain a Jpeg image and starts with 0xff 0xd8. Any idea why this might be happening?
codinguser