BitmapExtensionsTransformColors(Bitmap, FuncColor32, Color32, IDitherer, Color, Byte) Method

Transforms the colors of a bitmap using the specified transformFunction delegate.


Namespace: KGySoft.Drawing
Assembly: KGySoft.Drawing (in KGySoft.Drawing.dll) Version: 7.1.0
public static void TransformColors(
	this Bitmap bitmap,
	Func<Color32, Color32> transformFunction,
	IDitherer? ditherer,
	Color backColor = default,
	byte alphaThreshold = 128


bitmap  Bitmap
The Bitmap to be transformed.
transformFunction  FuncColor32, Color32
The transform function to be used on the colors of the specified bitmap. It must be thread-safe.
ditherer  IDitherer
An optional IDitherer instance to dither the result of the transformation if transformFunction returns colors that is not compatible with the PixelFormat of the specified bitmap.
backColor  Color  (Optional)
If transformFunction returns colors with alpha and bitmap has no alpha or supports single bit alpha only, then specifies the color of the background. Color values with alpha, which are considered opaque will be blended with this color before setting the pixel in the specified bitmap. The alpha value (Color.A property) of the specified background color is ignored. This parameter is optional.
Default value: Color.Empty, which has the same RGB values as Black.
alphaThreshold  Byte  (Optional)
If transformFunction returns colors with alpha and bitmap supports single bit alpha only, then specifies a threshold value for the Color32.A field, under which the color is considered transparent. If 0, then the pixels to be set will never be transparent. This parameter is optional.
Default value: 128.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type Bitmap. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).


This method transforms the bitmap in place (its original content will be overwritten). To return a new instance use the ConvertPixelFormat extension method with an IQuantizer instance created by the PredefinedColorsQuantizer.FromCustomFunction method.

If bitmap has an indexed PixelFormat and ditherer is , then its palette entries will be transformed instead of the actual pixels. To transform the colors of an indexed Bitmap without changing the palette specify a non- ditherer. Transforming the palette is both faster and provides a better result.

On multi-core systems transformFunction might be called concurrently so it must be thread-safe.

The ditherer is ignored for PixelFormats with more than 16 bits-per-pixel and for the Format16bppGrayScale format.


The following example demonstrates how to use this method:
using Bitmap original = Icons.Shield.ExtractBitmap(new Size(256, 256));

// starting with an indexed image using an optimized 8 BPP palette
using Bitmap bmp = original.ConvertPixelFormat(PixelFormat.Format8bppIndexed,


// Transforming colors to grayscale. By specifying a ditherer the original palette is preserved
// (which is not so optimal for the transformed image anymore). The ditherer tries
// to approximate the desired result with the original palette as much as possible.
// Try it also without a ditherer to transform only the palette entries.
bmp.TransformColors(c => c.ToGray(),
    ErrorDiffusionDitherer.FloydSteinberg.ConfigureErrorDiffusionMode(byBrightness: true));

// As ditherer was not null now the result is generated using the original palette

The example above produces the following results:

before.gifShield icon quantized to 256 colors using the Median Cut algorithm
after.gifShield icon transformed to grayscale with Floyd-Steinberg dithering while still using an optimized palette for the colored version


ArgumentNullExceptionbitmap or transformFunction is .

See Also