Reduce size of PNG with minimal quality loss

Jun 15, 2015 at 6:27 PM
Edited Jun 15, 2015 at 6:28 PM
I'm trying to reduce the size of PNG image using following code:
using (var bitmap = new MagickImage("test.png"))
    bitmap.Resize(800, 600);
    bitmap.Quality = 100;
    bitmap.Format = MagickFormat.Png;
But I see that image loses quality tragically.
What is the best way to reduce the size of PNG image with minimal quality loss?
Jun 15, 2015 at 7:33 PM
Two questions(in addition to question above):
1 How can I convert image to sRGB color profile?
Is this code correct?
2 How can I determine which color profile used for image?
var profile = bitmap.GetColorProfile();
// profile.Name always equals 'icc'
Jun 15, 2015 at 7:36 PM
Edited Jun 15, 2015 at 8:02 PM
You might want to try and disable MagickNET.OpenCL, that is the only thing that could affect the Quality. You will probably get a better result without it. And the Quality property for PNG does not work how you think it does. Here is an explanation:
  /* Untangle the "-quality" setting:

     Undefined is 0; the default is used.
     Default is 75

     10's digit:

        0 or omitted: Use Z_HUFFMAN_ONLY strategy with the
           zlib default compression level

        1-9: the zlib compression level

     1's digit:

        0-4: the PNG filter method

        5:   libpng adaptive filtering if compression level > 5
             libpng filter type "none" if compression level <= 5
                or if image is grayscale or palette

        6:   libpng adaptive filtering

        7:   "LOCO" filtering (intrapixel differing) if writing
             a MNG, otherwise "none".  Did not work in IM-6.7.0-9
             and earlier because of a missing "else".

        8:   Z_RLE strategy (or Z_HUFFMAN_ONLY if quality < 10), adaptive
             filtering. Unused prior to IM-6.7.0-10, was same as 6

        9:   Z_RLE strategy (or Z_HUFFMAN_ONLY if quality < 10), no PNG filters
             Unused prior to IM-6.7.0-10, was same as 6

    Note that using the -quality option, not all combinations of
    PNG filter type, zlib compression level, and zlib compression
    strategy are possible.  This will be addressed soon in a
    release that accomodates "-define png:compression-strategy", etc.

And an answer to your other question are:

1: That is correct but that only works if your source image also has a color profile. In the next version of Magick.NET I have added the following new method to make this easier:
/// <summary>
///  Transforms the image from the colorspace of the source profile to the target profile. The
///  source profile will only be used if the image does not contain a color profile. Nothing
///  will happen if the source profile has a different colorspace then that of the image.
/// </summary>
/// <param name="source">The source color profile.</param>
/// <param name="target">The target color profile</param>
public void TransformColorSpace(ColorProfile source, ColorProfile target)
2: You can use image.GetAttribute("icc:description") for this but I will try to see if I can add this to the color profile class.
Jun 16, 2015 at 12:35 PM
Jun 26, 2015 at 11:32 AM
how can i disable MagickNET.OpenCL ?

Best Regards
Gennaro Russo
Jun 26, 2015 at 11:37 AM
MagickNET.UseOpenCL = false;