Converting a byte array to PNG/JPG

121,497

Solution 1

You should be able to do something like this:

using System.Drawing.Imaging;
using System.Drawing;
using System.IO;

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.

Solution 2

I like Imagemagick. http://www.imagemagick.org/script/api.php

Solution 3

There are two problems with this question:

Assuming you have a gray scale bitmap, you have two factors to consider:

  1. For JPGS... what loss of quality is tolerable?
  2. For pngs... what level of compression is tolerable? (Although for most things I've seen, you don't have that much of a choice, so this choice might be negligible.) For anybody thinking this question doesn't make sense: yes, you can change the amount of compression/number of passes attempted to compress; check out either Ifranview or some of it's plugins.

Answer those questions, and then you might be able to find your original answer.

Share:
121,497
user472875
Author by

user472875

Updated on October 24, 2021

Comments

  • user472875
    user472875 over 2 years

    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