BitmapExtensionsAdjustBrightness Method

Adjusts the brightness of the specified bitmap.

See the online help for some examples with images.

Definition

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

Parameters

bitmap  Bitmap
The Bitmap to be transformed.
brightness  Single
A float value between -1 and 1, inclusive bounds. Positive values make the bitmap brighter, while negative values make it darker.
ditherer  IDitherer  (Optional)
An optional IDitherer instance to dither the result of the transformation if the transformed colors are not compatible with the PixelFormat of the specified bitmap. This parameter is optional.
Default value: .
channels  ColorChannels  (Optional)
The ColorChannels, on which the adjustment has to be performed. This parameter is optional.
Default value: Rgb.

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

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.AdjustBrightness(-0.5f);
bmp.SaveAsPng(@"c:\temp\after.png");

The example above produces the following results:

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

Example 2: Adjusting the brightness of an indexed bitmap with dithering

By default, adjusting the brightness of an indexed bitmap transforms the palette entries only, which is both faster and provides a better result. To force using the original palette, you can specify a non- ditherer:

C#
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");

// Making the image darker. 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.
// Try different brightness values and ColorChannels, too.
bmp.AdjustBrightness(-0.5f, ErrorDiffusionDitherer.FloydSteinberg, ColorChannels.Rgb);

// 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.gifShield icon quantized to 256 colors using the Median Cut algorithm
after.gifShield icon transformed to be darker with Floyd-Steinberg dithering while still using a palette optimized for the original image

Example 3: 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.AdjustBrightness 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.AdjustBrightness(-0.5f);
bmp.SaveAsPng(@"c:\temp\after.png");

The example above produces the following results:

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

Exceptions

ArgumentNullExceptionbitmap is .
ArgumentOutOfRangeExceptionbrightness is not between -1 and 1
-or-
channels is out of the defined flags.

See Also