C#: Seeking PNG Compression algorithm/library [closed]
I need to compress or at least drop the quality of some png images that users are uploading to my site. I already resized it but that doesn't do much for the image size.
Seeking a png/image compression or quality loss algorithm or library for .net 4.0 or under.
This is how I currently save/convert my images:
Image mainImg = ImageHelper.ResizeImage(bmp, 600, 500, false);
mainImg.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
Solution 1:
Unfortunately, .NET (and GDI+, which the .NET graphics libraries are built on) does not support any encoding parameters for PNG. In fact, if you call GetEncoderParameterList on your image with the PNG encoder Clsid, you'll get a "Not implemented" exception.
Even more unfortunate is that both ImageFormat and ImageCodecInfo are sealed classes and you can't just add new codecs to what .NET can natively access. Microsoft dropped the ball on this one.
This means your alternatives are, in decreasing order of masochism: 1) Implement a save function on your own in .NET that implements RFC 2083, 2) Implement a save function based off porting libpng to .NET, 3) Call unmanaged code that was built from libpng directly
libpng has great documentation and is free, both in availability and license permissiveness.
Solution 2:
PNG is generally a lossless compression scheme, which means image quality is never reduced unless you reduce the pixel count (size) or color depth. There are three ways to reduce the file size of PNG images:
- Reduce the pixel count (by downscaling the image); there's an obvious loss of resolution here
- Using a different precompression filters/DEFLATE compressor parameters (OptiPNG is my favorite tool to automatically choose the best combination of filters/compressor settings)
- Reducing the color depth from true color to 256 (or less) colors will give you substantial byte savings, but usually at great visual cost (especially for photographic images)
It seems like .NET doesn't have a built-in way to change precompression filters or compressor parameters of PNGs, so you'll have to use an external library. OptiPNG or any of the other PNG optimization tools aren't native .NET libraries, so you'll have to deal with P/Invoking the libraries or running a separate process to.. process each image. However, you're often looking at a savings around 5% or less (although I've seen as much as 40%) since this is ultimately a lossless process.
I'd recommend against reducing color depth in the automated scenario you describe, because the results can look truly awful (think dithered GIFs of old). (However, If you're doing manual optimization, look at Color Quantizer on Windows and ImageAlpha on Mac.)
Realistically, the best way you can reduce file size at the expense of quality is to just convert to JPEG, which requires no external dependencies, and is designed to be a lossy compression algorithm:
mainImg.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg); // make sure you change `filePath` to end with jpg instead of png.