BitmapSourceExtensionsConvertPixelFormat(BitmapSource, PixelFormat, Color, Color, Byte) Method

Converts the specified bitmap to a WriteableBitmap of the desired PixelFormat.

Definition

Namespace: KGySoft.Drawing.Wpf
Assembly: KGySoft.Drawing.Wpf (in KGySoft.Drawing.Wpf.dll) Version: 10.0.0-rc.1
C#
public static WriteableBitmap ConvertPixelFormat(
	this BitmapSource bitmap,
	PixelFormat newPixelFormat,
	Color[]? palette,
	Color backColor = default,
	byte alphaThreshold = 128
)

Parameters

bitmap  BitmapSource
The original bitmap to convert.
newPixelFormat  PixelFormat
The desired new pixel format.
palette  Color
The desired target palette if newPixelFormat is an indexed format. If , then the source palette is taken from the source image if it also has a palette of no more entries than the target indexed format can have; otherwise, a default palette will be used based on newPixelFormat.
backColor  Color  (Optional)
If newPixelFormat does not support alpha, then specifies the color of the background. Source pixels with alpha, which will be opaque in the result will be blended with this color. The Color.A property of the background color is ignored. This parameter is optional.
Default value: The bitwise zero instance of Color, which has the same RGB values as Black.
alphaThreshold  Byte  (Optional)
If newPixelFormat is an indexed format and the target palette contains a transparent color, then specifies a threshold value for the Color.A property, under which the color is considered transparent. If 0, then the result will not have transparent pixels. This parameter is optional.
Default value: 128.

Return Value

WriteableBitmap
A new WriteableBitmap instance with the desired pixel format.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type BitmapSource. 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

If newPixelFormat can represent fewer colors than the source format, then a default quantization will occur during the conversion. To use a specific quantizer (and optionally a ditherer) use the ConvertPixelFormat(BitmapSource, PixelFormat, IQuantizer, IDitherer) overload. To use a quantizer with a specific palette you can use the PredefinedColorsQuantizer class.

If newPixelFormat is Indexed8, bitmap has no palette and palette is , then a default 256 color palette will be used containing the 16 standard basic sRGB colors, the "web-safe" palette of 216 colors as well as 24 transparent entries.

If newPixelFormat is Indexed4, bitmap has no palette and palette is , then the 16 standard basic sRGB colors will be used.

If newPixelFormat is Indexed2, bitmap has no palette and palette is , then the palette will consist of 4 grayscale colors, containing black, white and the two gray entries that present in the default 4-bit palette.

If newPixelFormat is Indexed1, bitmap has no palette and palette is , then black and white colors will be used.

Exceptions

ArgumentNullExceptionbitmap is .
ArgumentOutOfRangeExceptionnewPixelFormat does not specify a valid format.
ArgumentExceptionpalette contains too many colors for the indexed format specified by newPixelFormat.
InvalidOperationExceptionA deadlock has been detected while attempting to create the result.

See Also