nquant icon indicating copy to clipboard operation
nquant copied to clipboard

The image you are attempting to quantize does not contain a 32 bit ARGB palette.

Open shatulsky opened this issue 4 years ago • 1 comments

https://github.com/drewnoakes/nquant/blob/be8bc2e78ea412853629e8dbf98e38bed2853f72/nQuant.Core/ImageBuffer.cs#L21-L23

There is actually a workaround for this case.

In my case converting the image to Format32bppPArgb and then using Quantize on it - decreased original image size by ~3 times.

Here is my code example

public static byte[] CompressImageStream(byte[] imageStream)
{
    using (var ms = new MemoryStream(imageStream))
    using (var original = new Bitmap(ms))
    using (var clonedWith32PixelsFormat = new Bitmap(
        original.Width,
        original.Height,
        PixelFormat.Format32bppPArgb))
    {
        using (Graphics gr = Graphics.FromImage(clonedWith32PixelsFormat))
        {
            gr.DrawImage(
                original,
                new Rectangle(0, 0, clonedWith32PixelsFormat.Width, clonedWith32PixelsFormat.Height));
        }

        using (Image compressedImage = new WuQuantizer().QuantizeImage(clonedWith32PixelsFormat))
        {
            return ImageToByteArray(compressedImage);
        }
    }
}

public static byte[] ImageToByteArray(Image image)
{
    if (image == null)
    {
        throw new ArgumentNullException(nameof(image));
    }

    using (var stream = new MemoryStream())
    {
        image.Save(stream, ImageFormat.Png);
        return stream.ToArray();
    }
}

shatulsky avatar Feb 25 '20 16:02 shatulsky

Looks good. Could you create a pull request?

drewnoakes avatar Feb 27 '20 23:02 drewnoakes