63

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

afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
user472875
  • 3,115
  • 4
  • 37
  • 68
  • It's not clear what you mean - what is this byte array to start with? Could you give more details? – Jon Skeet Jan 20 '12 at 19:32
  • Your question isn't entirely clear. What does the `byte[]` represent? Is it the content of the actual image? Is it an uncompressed Bitmap that you want to save as JPG / PNG? – vcsjones Jan 20 '12 at 19:32
  • The question is just request for library recommendation. If you feel that it needs to be re-opened please edit the question so it does not look like recommendation request. Even then probably duplicate of https://stackoverflow.com/questions/15270844/how-can-i-convert-byte-to-bitmapimage/15272528 – Alexei Levenkov Feb 06 '20 at 03:32

3 Answers3

99

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.

Aage
  • 5,932
  • 2
  • 32
  • 57
Garrett Vlieger
  • 9,354
  • 4
  • 32
  • 44
  • 1
    +1...pretty much the Usual Way of doing it. Depending on the user's requirements, though, no need to save it to the files system. – Nicholas Carey Jan 20 '12 at 19:51
  • Perfect. I love it! This was _exactly_ what I was looking for. – kayleeFrye_onDeck Feb 12 '15 at 00:30
  • 3
    Hi, I'm trying similar thing to convert raw image to jpeg butgetting *Parameter is not valid* exception. Any ideas? `byte[] bytes = System.IO.File.ReadAllBytes(@"C:\Input.CR2"); using (Image image = Image.FromStream(new MemoryStream(bytes))) { image.Save(@"C:\output.jpg", ImageFormat.Jpeg); }` – huMpty duMpty Feb 08 '16 at 21:05
  • Look here: http://stackoverflow.com/questions/6333681/c-sharp-parameter-is-not-valid-creating-new-bitmap. Could be that the file is too large. – Garrett Vlieger Feb 08 '16 at 21:35
  • 2
    Why convert to `Image` first instead of just wiring bytes directly to file so long as you know the file type and include the correct extension? – markmnl Dec 14 '16 at 13:06
  • @markmnl Because the Byte array may not contain the header information needed to open the file. – KevinA Dec 11 '17 at 16:04
1

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.

JayC
  • 7,053
  • 2
  • 25
  • 41
1

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

ahoffer
  • 6,347
  • 4
  • 39
  • 68
  • I know this is for using the programming library, but what would the command line for the tool in that case ? – user2284570 Jul 16 '15 at 12:28
  • @user2284570 If you are using it on the command line, then you would be working with a file and not a byte array. Assuming that your input is PNG (a PNG signature followed by chunks), then `convert input.png output.jpg` would probably work. – ahoffer Dec 04 '17 at 16:59