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

KGy SOFT Drawing Libraries Help
Transforms the colors of a bitmap using the specified transformFunction delegate.
See the Remarks section for details.

Namespace:  KGySoft.Drawing
Assembly:  KGySoft.Drawing (in KGySoft.Drawing.dll) Version: 5.1.0

public static void TransformColors(
	this Bitmap bitmap,
	Func<Color32, Color32> transformFunction,
	Color backColor = null,
	byte alphaThreshold = 128


Type: System.DrawingBitmap
The Bitmap to be transformed.
Type: SystemFuncColor32, Color32
The transform function to be used on the colors of the specified bitmap. It must be thread-safe.
backColor (Optional)
Type: System.DrawingColor
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 (Optional)
Type: SystemByte
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).

ArgumentNullExceptionbitmap or transformFunction is .

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, then its palette entries will be transformed instead of the actual pixels.

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

Tip Tip
If transformFunction can return colors incompatible with the pixel format of the specified bitmap, or you want to transform the actual pixels of an indexed Bitmap instead of modifying the palette, then use the TransformColors(Bitmap, FuncColor32, Color32, IDitherer, Color, Byte) overload and specify an IDitherer instance.
See Also