BitmapExtensionsTransformColors(Bitmap, FuncColor32, Color32, IDitherer, Color, Byte) Method
Transforms the colors of a bitmap using the specified transformFunction delegate.
Namespace: KGySoft.DrawingAssembly: KGySoft.Drawing (in KGySoft.Drawing.dll) Version: 8.1.0
public static void TransformColors(
this Bitmap bitmap,
Func<Color32, Color32> transformFunction,
IDitherer? ditherer,
Color backColor = default,
byte alphaThreshold = 128
)
<ExtensionAttribute>
Public Shared Sub TransformColors (
bitmap As Bitmap,
transformFunction As Func(Of Color32, Color32),
ditherer As IDitherer,
Optional backColor As Color = Nothing,
Optional alphaThreshold As Byte = 128
)
public:
[ExtensionAttribute]
static void TransformColors(
Bitmap^ bitmap,
Func<Color32, Color32>^ transformFunction,
IDitherer^ ditherer,
Color backColor = Color(),
unsigned char alphaThreshold = 128
)
[<ExtensionAttribute>]
static member TransformColors :
bitmap : Bitmap *
transformFunction : Func<Color32, Color32> *
ditherer : IDitherer *
?backColor : Color *
?alphaThreshold : byte
(* Defaults:
let _backColor = defaultArg backColor new Color()
let _alphaThreshold = defaultArg alphaThreshold 128
*)
-> unit
- 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.
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 adjusts the degree of parallelization automatically, blocks the caller, and does not support cancellation or reporting progress. Use the
BitmapDataExtensions.BeginTransformColors
or
BitmapDataExtensions.TransformColorsAsync (in .NET Framework 4.0 and above) methods for asynchronous call and to adjust parallelization, set up cancellation and for reporting progress.
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,
OptimizedPaletteQuantizer.MedianCut());
bmp.SaveAsGif(@"c:\temp\before.gif");
// 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
bmp.SaveAsGif(@"c:\temp\after.gif");
The example above produces the following results:
before.gif | |
after.gif | |