BitmapExtensionsInvert Method

Inverts the colors of the specified bitmap.

See the online help for a couple of examples with images.

Definition

Namespace: KGySoft.Drawing
Assembly: KGySoft.Drawing (in KGySoft.Drawing.dll) Version: 10.0.0-rc.1
C#
public static void Invert(
	this Bitmap bitmap,
	IDitherer? ditherer = null
)

Parameters

bitmap  Bitmap
The Bitmap to be inverted.
ditherer  IDitherer  (Optional)
An optional IDitherer instance to dither the result of the transformation if the inverse of the bitmap has no exact representation with its PixelFormat. This parameter is optional.
Default value: .

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).

Remarks

  Note

This method adjusts the degree of parallelization automatically, blocks the caller, and does not support cancellation or reporting progress. Use the BitmapDataExtensions.BeginInvert or BitmapDataExtensions.InvertAsync (in .NET Framework 4.0 and above) methods for asynchronous call and to adjust parallelization, set up cancellation and for reporting progress.

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.

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

Examples

Example 1: Simple usage

C#
using Bitmap bmp = Icons.Shield.ExtractBitmap(new Size(256, 256));
bmp.SaveAsPng(@"c:\temp\before.png");
bmp.Invert();
bmp.SaveAsPng(@"c:\temp\after.png");

The example above produces the following results:

before.pngWindows shield icon
after.pngShield icon inverted in the sRGB color space

Example 2: Using linear color space

This method uses the color space that naturally matches the pixel format of the bitmap. In Example 1 the original bitmap had a 32 BPP pixel format, so the sRGB color space was used. To specify a color space explicitly, you can obtain an IReadWriteBitmapData instance and use the BitmapDataExtensions.Invert method instead:
C#
using Bitmap bmp = Icons.Shield.ExtractBitmap(new Size(256, 256));
bmp.SaveAsPng(@"c:\temp\before.png");
using (IReadWriteBitmapData bitmapData = bmp.GetReadWriteBitmapData(WorkingColorSpace.Linear))
    bitmapData.Invert();
bmp.SaveAsPng(@"c:\temp\after.png");

The example above produces the following results:

before.pngWindows shield icon
after.pngShield icon inverted in the linear color space

See Also