AForge.Imaging.ColorReduction.ColorImageQuantizer.ReduceColors C# (CSharp) Method

ReduceColors() public method

Create an image with reduced number of colors.

The method creates an image, which looks similar to the specified image, but contains reduced number of colors. First, target color palette is calculated using CalculatePalette(UnmanagedImage, int) method and then a new image is created, where pixels from the given source image are substituted by best matching colors from calculated color table.

The output image has 4 bpp or 8 bpp indexed pixel format depending on the target palette size - 4 bpp for palette size 16 or less; 8 bpp otherwise.

Unsupported format of the source image - it must 24 or 32 bpp color image. Invalid size of the target color palette.
public ReduceColors ( UnmanagedImage image, int paletteSize ) : Bitmap
image AForge.Imaging.UnmanagedImage Source image to process.
paletteSize int Number of colors to get in the output image, [2, 256].
return System.Drawing.Bitmap
        public Bitmap ReduceColors( UnmanagedImage image, int paletteSize )
        {
            if ( ( paletteSize < 2 ) || ( paletteSize > 256 ) )
            {
                throw new ArgumentException( "Invalid size of the target color palette." );
            }

            return ReduceColors( image, CalculatePalette( image, paletteSize ) );
        }

Same methods

ColorImageQuantizer::ReduceColors ( Bitmap image, Color palette ) : Bitmap
ColorImageQuantizer::ReduceColors ( Bitmap image, int paletteSize ) : Bitmap
ColorImageQuantizer::ReduceColors ( UnmanagedImage image, Color palette ) : Bitmap

Usage Example

 public static Bitmap MedianCut(Bitmap bmp, int intensity)
 {
     ColorImageQuantizer ciq = new ColorImageQuantizer(new MedianCutQuantizer());
     IColorQuantizer quantizer = new MedianCutQuantizer();
     Bitmap newImage = ciq.ReduceColors(bmp, intensity);
     return newImage;
 }