OptimizedPaletteQuantizerMedianCut Method

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

See the online help for an example with images.

Definition

Namespace: KGySoft.Drawing.Imaging
Assembly: KGySoft.Drawing.Core (in KGySoft.Drawing.Core.dll) Version: 9.0.0
C#
public static OptimizedPaletteQuantizer MedianCut(
	int maxColors = 256,
	Color32 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  Color32  (Optional)
Colors with alpha above the alphaThreshold will be blended with this color before quantizing. The Color32.A field of the background color is ignored. This parameter is optional.
Default value: The default value of the Color32 type, which has the same RGB values as Color.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 can quantize colors of an image using the Median Cut quantizing algorithm.

Examples

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