17

I am looking to build in PNG optimization (a PNG "sqeezer/squisher" of sorts) into my builds (TFS 2010 Build Workflow). I want to create a new build activity and as with all my activities, I prefer to have all my code managed with little to no dependencies.

I have searched (Googled) and found many different PNG libraries for PNG optimiaztion. PNGOUT, PNGCRUSH, OptiPNG, etc. None of them are really .NET. They all seem like great tools. I prefer not to call EXE files (separate processes) from my code activity because you never really know what will happen (less reliable).

So i thought maybe PNGGauntlet would be an option. But looking quickly at the installed application, its merely an C# GUI that calls the binary pngout.exe. That is exactly what I don't want to do.

So my question is this : Is there an open-source and/or free .NET library (DLL) that does PNG compression?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Issa Fram
  • 2,556
  • 7
  • 33
  • 63
  • 1
    Have you tried http://madskristensen.net/post/Image-Optimizer-%28beta%29-VS2010-extension.aspx ? It's not really a library but I find it nice to be integrated with VS. – Ron Warholic Sep 09 '11 at 20:32
  • Not a pure C# solution, but see http://stackoverflow.com/q/2075084/2291 for some of those external programs Issa was talking about. – Jon Adams Sep 09 '11 at 22:00
  • It was a year old OT (or as it used to be called, "not constructive") question with low views. Rather than close I just deleted. Can't guarantee it will hang around for long (the system automatically removes these after some time, not sure of the algo tho), but you should always be able to get here if you have a link, so I'd suggest you bookmark it. –  Jul 01 '13 at 19:17
  • @Will It was just never marked as resolved. Now it is marked. Can you remove the hold? Thank you! – Issa Fram Jul 01 '13 at 21:04
  • @IssaFram sorry, we close these down permanents. We may be (terminally) slow, but as you can see from the close reason (not custom!) we actively discourage these types here. Please take the remarkable lack of spam here (there is tons you won't see before reaching 10k) as an indication why this is policy. –  Jul 02 '13 at 03:15
  • @Will this question was not off-topic though. I wasn't asking for a recommendation of a tool. I was asking if a tool was available to solve a specific problem. My question was edited a while back to make that even clearer. It spawned off great answers. None of them were spam. – Issa Fram Jul 02 '13 at 14:56
  • **"Questions asking us to recommend a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it."** Not sure how much clearer I can put that. If you are really having issues with this, please go to [meta] and ask a question on the subject. –  Jul 02 '13 at 14:57
  • @Will Seems like this isn't going anywhere but I must quote a part of the question "So my question is this : Is there an open-source and/or free .NET library (DLL) that does PNG compression?". I wasn't asking for a recommendation or a favorite. I also described the problem I was having. Just like the quoted policy you had described. Regardless, thank you for your time. – Issa Fram Jul 02 '13 at 18:03

4 Answers4

25

I've just found this one: nQuant. From the project page:

nQuant is a .net color quantizer that produces high quality 256 color 8 bit PNG images. nQuant often reduces the size of a 32 bit image to a size 3 times smaller than its original with quality surpassing what the standard C command line utilities produce.

I've tested it out with some sample PNGs and it really does produce a good 8-bit output, smaller than .NET's own 8-bit encoder and with no visible dithering, even for antialiased images. I miss one thing, however: the processing can be a bit slow, but the library unfortunately doesn't have a parallelized variant of the optimization code. I'm working on a solution for this.

UPDATE (Jan 21, 2013), I've refactored the code, it runs 10 times faster now, even without the parallel threads. The fork is here: http://nquant.codeplex.com/SourceControl/network/forks/breki74/nQuantFaster

UPDATE (Feb 24, 2013), my fork was merged to the trunk. Enjoy.

rae1
  • 6,066
  • 4
  • 27
  • 48
Igor Brejc
  • 18,714
  • 13
  • 76
  • 95
11

Just in case you decide to write your own C# code: I've writen an open source pure Java PNG coder/decoder, PNGJ, you might find it useful; it should not be difficult to port to C#. It's just a coder, not an optimizer, but it supports all writing options/strategies (including different filters for each row) so you could easily plug your heuristics to it.

Updated: I've coded a C# PNG coder/encoder PngCs, ported from Java (PngJ), it's open source and, since Dec-2012 it supports all PNG variants (except that it does not write interlaced PNG, only reads them)

leonbloy
  • 73,180
  • 20
  • 142
  • 190
  • i'll be the first to admit that the domain of image encoding/decoding and optimizing is definitely not my area of expertise. This is great for doing later, but my needs are more time sensitive. Thank you though. – Issa Fram Sep 12 '11 at 19:10
  • 1
    Haha someone started this not too long after this question... http://code.google.com/p/pngcs/ – Issa Fram Oct 28 '11 at 06:14
  • 1
    @IssaFram that's someone is me :-) – leonbloy Oct 28 '11 at 10:47
  • Thank god that you did this. :-) – Felix K. Oct 15 '12 at 14:42
  • Would be cool if you used the native zip functionality or one of the open source zlib (pure c#) libraries instead of `ICSharpCode.SharpZipLib.dll` . For those coming late the this party, @leonbloy has a github repo of this located here > https://github.com/leonbloy/pngcs – Kraang Prime Aug 13 '16 at 16:36
  • @SamuelJackson At that time the native zip library was useless (and I don't think there was a better open source zlib) - only after .Net 4.5 (I think) ICSharpCode.SharpZipLib.dl became unnecessary. I need to update the library, yes. – leonbloy Aug 13 '16 at 17:10
  • @leonbloy - correct. There was a better library from [XceedSoft](https://dotnetzip.codeplex.com/) that was free (and since i have been using xceed zip since shortly after that company was founded, i do find it much better), but totally understand why it's using that lib :). The xeed one was a pita to find though, I only happened accross it when searching for something else, then saw who made it, and was like... ZOMG !!! – Kraang Prime Aug 13 '16 at 23:18
7

After a fairly decent Google search and spotting one or two of these questions on StackOverflow recently, I don't believe there is a C# library available.

However, OptiPNG is open source C code, so porting to C# is a possibility. Alternatively, pull the C code out into a separate library outside of an exe and P/Invoke into it.

There appear to be other open source libraries around, not necessarily specifically for optimization:

http://www.libpng.org/pub/png/pngcode.html

Apologies for the anti-climax, I've been after one of these too and I've come to the conclusion the current answer is one doesn't exist. I eagerly wait to be corrected.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
-1

According to this answer there are several .net libraries that can be used to optimize the png size

The tools I use for png optimization are:

  1. Convert to png8: If there are few colors (say, screenshots) then I use [pngnq][2] or Gimp's Indexed color mode to quantize down to 256 colors. Png8 can be smaller than png24 or png32. For details see [PNG8 – The Clear Winner][3].
  2. Optipng, a fast general png optimizer. [C# PNG Optimization Tutorial][9] has details on how to run optipng from C#.
  3. Finally [pngout][4] is slow but often (80-90% of the time) manages to squeeze the png down further than optipng. Run optipng first though, as optipng will automatically do other optimizations that pngout does not attempt.
Community
  • 1
  • 1
Korayem
  • 12,108
  • 5
  • 69
  • 56
  • looks like you are missing several links, also, the OP made it clear they did not want to use 3rd party tools (like exe's and such), and that the optimizer *must* be open source AND written in c#. Simply running an arbitrary 3rd party exe, is clearly not a "pure c#" solution. – Kraang Prime Aug 13 '16 at 16:28