4

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
1
  • 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
2

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.

0

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)

1
  • 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.