---
title: "Png NeuQuant Example"
canonical: https://www.graphicsmill.com/samples/Codecs/Png/PngNeuQuantExample_cs
product: "Graphics Mill"
source: "Codecs/Png/PngNeuQuantExample.cs"
---

# Png NeuQuant Example

Saves image to PNG format using different bitness, color quantization and dithering algorithms.

## Instruction

Write a C# program that saves an image to PNG format using different bit depths, color quantization algorithms, and dithering techniques. The program should convert the image to 24-bit PNG, 8-bit PNG with Octree and Floyd-Steinberg dithering, and 8-bit PNG with NeuQuant color quantization.

## Usings

```csharp
using System;
using Aurigma.GraphicsMill.Codecs;
```

## Code Snippet

```csharp
public static void Run()
{
    Save("Chicago.jpg");
    Save("Stamp.png");
}

private static void Save(string sourceFileName)
{
    // Source file information
    long sourceFileSize = new System.IO.FileInfo(sourceFileName).Length;

    Console.WriteLine("Source file:");
    Console.WriteLine("  {0}", sourceFileName);
    Console.WriteLine("  {0:f2} KB", sourceFileSize / 1024f);

    var baseName = System.IO.Path.GetFileNameWithoutExtension(sourceFileName);

    // Convert to 24-bit PNG
    {
        var png24bitFileName = baseName + "_24bit.png";

        // Save to PNG
        using (var bitmap = new Bitmap(sourceFileName))
        {
            bitmap.Save(png24bitFileName, new PngSettings(true));
        }

        long png24bitFileSize = new System.IO.FileInfo(png24bitFileName).Length;

        Console.WriteLine("PNG 24-bit:");
        Console.WriteLine("  {0}", png24bitFileName);
        Console.WriteLine("  {0:f2} KB ({1:f2}%)", (float)png24bitFileSize / 1024f, (float)png24bitFileSize / (float)sourceFileSize * 100f);
    }

    // Convert to 8-bit PNG using the Octree color quantization and Floyd–Steinberg dithering algorithms
    {
        var png8bitFileName = baseName + "_8bit.png";

        // Save to PNG
        using (var bitmap = new Bitmap(sourceFileName))
        {
            bitmap.ColorManagement.Convert(PixelFormat.Format8bppIndexed);

            bitmap.Save(png8bitFileName, new PngSettings(true));
        }

        long png8bitFileSize = new System.IO.FileInfo(png8bitFileName).Length;

        Console.WriteLine("PNG 8-bit (Octree, Floyd–Steinberg, no alpha transparency):");
        Console.WriteLine("  {0}", png8bitFileName);
        Console.WriteLine("  {0:f2} KB ({1:f2}%)", (float)png8bitFileSize / 1024f, (float)png8bitFileSize / (float)sourceFileSize * 100f);
    }

    // Convert to 8-bit PNG using the Neuquant color quantization algorithm
    {
        var png8bitNeuQuantFileName = baseName + "_8bit_NeuQuant.png";

        // Save to PNG
        using (var bitmap = new Bitmap(sourceFileName))
        {
            bitmap.ColorManagement.Palette = new ColorPalette(ColorPaletteType.NeuQuant);
            bitmap.ColorManagement.Convert(PixelFormat.Format8bppIndexed);

            bitmap.Save(png8bitNeuQuantFileName, new PngSettings(true));
        }

        long png8bitNeuQuantFileSize = new System.IO.FileInfo(png8bitNeuQuantFileName).Length;

        Console.WriteLine("PNG 8-bit (NeuQaunt, with alpha transparency):");
        Console.WriteLine("  {0}", png8bitNeuQuantFileName);
        Console.WriteLine("  {0:f2} KB ({1:f2}%)", (float)png8bitNeuQuantFileSize / 1024f, (float)png8bitNeuQuantFileSize / (float)sourceFileSize * 100f);
    }
}
```

## Explanation

Below is a C# code sample that demonstrates how to save an image to PNG format using various bit depths and color quantization methods. The program saves the image as a 24-bit PNG, an 8-bit PNG with Octree and Floyd-Steinberg dithering, and an 8-bit PNG with NeuQuant color quantization:

This code provides conversions that optimize file sizes and image quality based on the selected bit depth and quantization algorithms.

## Tags

- [Format Conversion](<https://www.graphicsmill.com/samples/tags/format-conversion_md>): Converting images between formats
- [Indexed format](<https://www.graphicsmill.com/samples/tags/indexed_md>): Working with indexed-color images
- [PNG](<https://www.graphicsmill.com/samples/tags/png_md>): Reading and writing PNG images

## Input

- [Chicago.jpg](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/Chicago_jpg>) (preview)
- [Stamp.png](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/Stamp_png>) (preview)

## Output

- [PngNeuQuant.txt](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/PngNeuQuant_txt>) (preview)
- [Chicago\_24bit.png](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/Chicago_24bit_png>) (preview)
- [Chicago\_8bit.png](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/Chicago_8bit_png>) (preview)
- [Chicago\_8bit\_NeuQuant.png](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/Chicago_8bit_NeuQuant_png>) (preview)
- [Stamp\_24bit.png](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/Stamp_24bit_png>) (preview)
- [Stamp\_8bit.png](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/Stamp_8bit_png>) (preview)
- [Stamp\_8bit\_NeuQuant.png](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/Stamp_8bit_NeuQuant_png>) (preview)

## Used APIs

- [Aurigma.GraphicsMill.Bitmap.ColorManagement](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Bitmap_ColorManagement.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PngSettings](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PngSettings.htm.md>)
- [Aurigma.GraphicsMill.ColorManagementProvider.Convert(PixelFormat)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_ColorManagementProvider_Convert_1_3e209c07.htm.md>)
- [Aurigma.GraphicsMill.ColorManagementProvider.Palette](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_ColorManagementProvider_Palette.htm.md>)
- [Aurigma.GraphicsMill.ColorPalette](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_ColorPalette.htm.md>)
- [Aurigma.GraphicsMill.ColorPaletteType](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_ColorPaletteType.htm.md>)
