The .NET framework does not provide any options regarding the compression of PNGs. While the built-in support is suitable for most tasks I had so far, I have high performance requirements which I hope to resolve by using less compression and compensate through higher memory usage.

I saw there are many PNG libraries available on NuGet, but the sheer amount of search results makes it hard for me to decide for one.

Have you worked with one of those libraries and can you recommend one that's really fast?

The target .NET framework is 4.5; higher versions are acceptable.

I have tried:

  • NuGet libpng but it does not support .NET 4.5
  • NuGet pngpp but it does not support .NET 4.5
  • NuGet lpng but it does not support .NET 4.5
  • NuGet ImageProcessor but it does not have a Save() method
  • NuGet NGraphics but the GetImage().SaveAsPng() method has no parameters to change the compression
  • For PNGS... I would like to know what level of compression is tolerable? You can change the amount of compression/number of passes attempted to compress; I have used Ifranview and some of it's plugins. They seems to be decent in performance. One more suggestion is ImageMagic, its available at imagemagick.org/script/index.php This would be my second option. – Raghu Ariga Sep 15 '16 at 15:34

You could try net-vips, the C# binding for libvips. It's a lazy, streaming, demand-driven image processing library, so it can do operations like this without needing to load the whole image. NetVips is compatible for .NET Standard >= 2.0 and .NET Framework >= 4.5.

You can convert a JPG image to PNG in this way:

Image image = Image.NewFromFile("image.jpg", access: "sequential");
image.WriteToFile("image.png", new VOption
    {"compression", 9} // zlib compression 1-9

See the libvips API docs for vips_pngsave (the operation invoked by WriteToFile) for details which arguments are supported.

| improve this answer | |

I would recommend using the LEADTOOLS Imaging Pro SDK for this. This library works with .NET 2.0 and newer (including .NET 4.5). This library is thread-safe. If you are trying to convert .NET bitmaps that are already loaded in memory, you can output to PNG like this:

using (Image dotNetImage = Image.FromFile(@"C:\Users\Public\Documents\LEADTOOLS Images\cannon.jpg"))
   using (RasterCodecs codecs = new RasterCodecs())
      codecs.Save(RasterImageConverter.ConvertFromImage(dotNetImage, ConvertFromImageOptions.None),
                  RasterImageFormat.Png, 0);

If you are looking to do a straight file conversion, you can do this in two lines of code like this:

using (RasterCodecs codecs = new RasterCodecs())
   codecs.Convert(@"C:\Users\Public\Documents\LEADTOOLS Images\cannon.jpg", @"C:\cannon.png", RasterImageFormat.Png, 0, 0, 0, null);

LEADTOOLS can also wrap raw bitmap data with the RasterImage class to output that to PNG as well.

Disclaimer: I am an employee of the company that wrote this library.

| improve this answer | |
  • So the ,0 ,0 ,0, null part influences the compression? – Thomas Weller Dec 6 '16 at 7:15
  • The linked website says $795 for a single license. Can you elaborate on how to get a gratis version? If there's no gratis version, the answer is not valid, since I tagged the question gratis. – Thomas Weller Dec 6 '16 at 7:17

Can I suggest you look into the ImageMagick, this library has been around since 1990 and is very mature. It is a native library with a .NET version available.

The command line version definitely supports specifying the compression level of a PNG; see this StackOverflow post. But I didn't find the exact code you would need in the .NET implementation.

.NET Version https://magick.codeplex.com/

As a side note it does support OpenCL as well but I don't know if the PNG saving code leverages this. (if it does it will drastically improve your saving time)

| improve this answer | |
  • I was unable to figure out how to take a screenshot with the library. Therefore I use a normal Bitmap to take the screenshot and then use a MagickImage to save the file. This increased CPU load from ~4% to ~10%, so I don't have any benefit. – Thomas Weller Oct 17 '16 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.