We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Why png compression make the image bigger than source file? (Aspose.Imaging)

Hello,

I have some misunderstanding with png image compression, I try to compress the png image with a size ~6.2Mb. I wrote the sample code to do it :

    using var image = Image.Load(src);
    using var options = new PngOptions()
    {
        CompressionLevel = 5
    };
    image.Save(dest, options);

after the compression, I got the png file with a size ~8.2Mb but my goal is to make the image a bit smaller than the source image ( in all levels of compression I got ~8.2Mb )

Also, I lost my transparent background ( it became black ). I played with configuration but it didn’t work for me.

can you explain why the image doesn’t compress?

I attache the sample image for testing
test image.zip (6.2 MB)

@AndreyKu You can get better results (around 6.6 MB) if you specify the following options:

using (RasterImage image = (RasterImage)Image.Load(inputFileName))
{
    PngOptions options = new PngOptions()
    {
        CompressionLevel = 9, // maximum compression level
        ColorType = PngColorType.TruecolorWithAlpha, // to keep transparency
        FilterType = PngFilterType.Avg, // best compression results in this specific case
    };

    image.Save(outputFileName, options);
}

Or you can get even better results (around 2 MB) if you save the image as an indexed one, but there will be no transparency:

using (RasterImage image = (RasterImage)Image.Load(inputFileName))
{
    PngOptions options = new PngOptions()
    {
        CompressionLevel = 9,
        ColorType = PngColorType.IndexedColor,
        Palette = ColorPaletteHelper.GetCloseImagePalette(image, 256),
        FilterType = PngFilterType.Avg,
    };

    image.Save(outputFileName, options);
}

We also have a corresponding internal ticket IMAGINGNET-4522 regarding saving of the indexed PNG images while keeping transparency, and you’ll be notified on progress.