0

Does such an encoder exist? The GDI+ one is not very fully featured. For example, it will always render out 32bpp images, even if you're using 8bpp indexed in-memory.

I'm about to start writing out a lot of PNGs from a web server that should really be 8bpp indexed with transparency (PNG supports alpha levels in the palette). I want to do this with native .NET (and unsafe code) for performance reasons, so please don't suggest ImageMagick or FreeImage.

The longer this question goes unanswered, the further through the PNG specification I will have read :)

2
  • Using a library written in c with p-invoke should rather be faster than writing in in C#. How about just p-invoking LibPNG? IMO the only reason to use C# is getting verifiable IL code that can run in low trust scenarios such as silverlight. Commented Nov 13, 2010 at 15:54
  • possible duplicate of A More precise PNG library for .NET 2.0?
    – ChrisF
    Commented Nov 13, 2010 at 15:57

2 Answers 2

2

Use the PngBitmapEncoder class, it uses WIC under the covers. The FormatConvertedBitmap class helps you set the palette and make the pixel format conversion. At least .NET 3.0 required.

1
  • thanks very much. The PngBitmapEncoder works quite well for what I want, though it's a shame I have to reference half of WPF in my ASP.NET program. Also, the Save method requires a seekable stream (as far as I can tell) so I have to write the file to a throw-away MemoryStream and then copy it out to the HTTP response stream. It'd be nice to avoid that memory churn. Still, I'm seeing a nice heat-map layer on Google Maps now. Thanks again. Commented Nov 13, 2010 at 23:04
1

You could also check out nQuant at http://nquant.codeplex.com. If you pass your GDI+ 32bit BitMap instance to it, it will return an 8bit 256 color indexed paelette image. If the original image was 8 bit, this conversion should be lossless.

This is a vanilla C# written library with no dependencies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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