Converting a byte array to PNG/JPG

C#PngJpeg

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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 - C#

Solution 3 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser472875View Question on Stackoverflow
Solution 1 - C#Garrett VliegerView Answer on Stackoverflow
Solution 2 - C#ahofferView Answer on Stackoverflow
Solution 3 - C#JayCView Answer on Stackoverflow