Converting a byte array to PNG/JPG

user472875 picture user472875 · Jan 20, 2012 · Viewed 107.9k times · Source

I am currently working on an application that requires high-performance conversion of an unpadded byte array to either a PNG or JPEG. The image format doesn't matter, just as long as it's fast.

I have tried the .NET libraries and the performance is very bad. Can anyone recommend a good freeware library for this?

EDIT: the byte[] is an 8bit grayscale bitmap

Answer

Garrett Vlieger picture Garrett Vlieger · Jan 20, 2012

You should be able to do something like this:

byte[] bitmap = GetYourImage();

using(Image image = Image.FromStream(new MemoryStream(bitmap)))
{
    image.Save("output.jpg", ImageFormat.Jpeg);  // Or Png
}

Look here for more info.

Hopefully this helps.