OptimizedPaletteQuantizerMedianCut Method

Gets an OptimizedPaletteQuantizer instance that quantizes colors of an image using the Median Cut quantizing algorithm.

Definition

Namespace: KGySoft.Drawing.Imaging
Assembly: KGySoft.Drawing.Core (in KGySoft.Drawing.Core.dll) Version: 8.1.0
C#
public static OptimizedPaletteQuantizer MedianCut(
	int maxColors = 256,
	Color backColor = default,
	byte alphaThreshold = 128
)

Parameters

maxColors  Int32  (Optional)
The upper limit of generated colors. Must be between 2 and 65536, inclusive bounds. This parameter is optional.
Default value: 256.
backColor  Color  (Optional)
Colors with alpha above the alphaThreshold will be blended with this color before quantizing. The Color.A property of the background color is ignored. This parameter is optional.
Default value: Empty, which has the same RGB values as Black.
alphaThreshold  Byte  (Optional)
Specifies a threshold value for the Color.A property, under which a quantized color is considered transparent. If 0, then the quantized colors will never be transparent. This parameter is optional.
Default value: 128.

Return Value

OptimizedPaletteQuantizer
A OptimizedPaletteQuantizer instance that quantizes colors of an image using the Median Cut quantizing algorithm.

Example

The following example demonstrates how to use the quantizer returned by this method:

  Note

This example requires to reference the KGySoft.Drawing package. When targeting .NET 7 or later it can be executed on Windows only.
C#
Bitmap bmpOriginal = Icons.Shield.ExtractBitmap(new Size(256, 256));
bmpOriginal.SaveAsPng(@"c:\temp\original.png");

IQuantizer quantizer = OptimizedPaletteQuantizer.MedianCut(256);
Bitmap bmpConverted = bmpOriginal.ConvertPixelFormat(PixelFormat.Format8bppIndexed, quantizer);
bmpConverted.SaveAsGif(@"c:\temp\converted.gif");

The example above produces the following result:

original.pngShield icon with transparent background
converted.gifShield icon quantized to 256 colors using the Median Cut algorithm

  Tip

For more image examples and side-by-side comparison with the other algorithms see the Remarks section of the OptimizedPaletteQuantizer class.

Exceptions

ArgumentOutOfRangeExceptionmaxColors must be between 2 and 65536, inclusive bounds.

See Also