3

I want to compress bitmaps to PNG with different compression levels as these levels are available in JPEG compression in C#. I have 20 to 30 images of different sizes to process in 1 sec. Is there any library to achieve this compression in PNG with different compression levels?

yoozer8
  • 7,361
  • 7
  • 58
  • 93
Ehtsham
  • 208
  • 1
  • 6
  • 14
  • Is [this](http://msdn.microsoft.com/en-us/library/aa970062.aspx) useful? If you already know how to create PNG and just want to optimize it, you can use [this tool](http://advsys.net/ken/util/pngout.htm) and execute it via `Process.Start()` method. – Shadow The GPT Wizard Aug 03 '11 at 06:41

2 Answers2

6

A lossless compression method does not have to be single quality level. I mean, some lossless compression methods have some parameters to choose speed/compression ratio trade-off. It's up to author.

As to PNG compression, it's actually bunch of filters and deflate algorithm. Considering deflate has a redundant encoding stage (e.g. two different compressors can produce completely different output, yet still they can be decompressed by any valid decompressor), it's no wonder several programs outputs different PNGs. Note that, I'm still not taking filters into account. There are several filters and there is no "best" filter i.e. it's quality varies by image.

Due to it's redundant feature, some people wrote PNG optimizers which take a regular PNG as input to produce smaller PNG without any perceptual loss. Some of them applies some tricks such as filling completely transparent area with some predictable colors to increase compression. But, in general they tweak parameters in a brute-force fashion.

Now, simplest answer for your question could be that you have two options:

  1. If you're going to run your executable on a desktop environment, you can use one of these tools as an optimizer. But, in shared hosting you can't use them due to possible privileges.
  2. You can write your own PNG writer in .NET that takes into account some brute-force parameter tuning.
Osman Turan
  • 1,361
  • 9
  • 12
  • *A lossless compression method does not have to be single quality level.* - If by quality, you are talking image quality, then you are incorrect. By definition it must provide the exact same quality. – D'Arcy Rittich Sep 21 '12 at 14:33
  • 2
    I think, it's clear that what I'm talking about compression level (i.e. output quality in terms of compressed size). More over, I've explicitly stated it: "I mean, some lossless compression methods have some parameters to choose speed/compression ratio trade-off.". – Osman Turan Sep 23 '12 at 08:38
  • *Quality* is not normally a word used with respect to file size. So no, it is not clear. – D'Arcy Rittich Sep 24 '12 at 02:50
  • Yeah, that should be "does not have to be single _compression_ level" – Nyerguds Aug 16 '16 at 21:43
0

According to this answer C#: Seeking PNG Compression algorithm/library PNG in C# is a lossless compression library, e.g. does not support multiple quality levels.

The wiki page on PNG (http://en.wikipedia.org/wiki/Portable_Network_Graphics) seems to confirm the compression format is lossless (e.g. has only a single compression level)

Some googling suggests there is research on lossy versions of PNG

Community
  • 1
  • 1
  • 1
    Maybe he is asking how to change bit depth? – deerchao Sep 10 '11 at 09:41
  • 1
    "seems to confirm the compression format is lossy" correct me if I'm wrong but png's are lossless and not lossy... – Peter Oct 14 '13 at 18:06
  • 4
    Compression level does _not_ equal quality level. For example, Winrar will always produce lossless compression (would be pretty bad for files transport otherwise), so its "quality level" is always 100%, but it _does_ have compression levels; higher compression will make the final file take less space, but will take longer to compress/decompress. – Nyerguds Aug 16 '16 at 21:38