SKBitmapExtensionsConvertPixelFormat(SKBitmap, IQuantizer, IDitherer, SKColorType, SKAlphaType, WorkingColorSpace) Method

Converts the pixel format of this bitmap using the specified colorType, alphaType and targetColorSpace.

Definition

Namespace: KGySoft.Drawing.SkiaSharp
Assembly: KGySoft.Drawing.SkiaSharp (in KGySoft.Drawing.SkiaSharp.dll) Version: 8.1.0
C#
public static SKBitmap ConvertPixelFormat(
	this SKBitmap bitmap,
	IQuantizer? quantizer,
	IDitherer? ditherer = null,
	SKColorType colorType = SKColorType.Unknown,
	SKAlphaType alphaType = SKAlphaType.Unknown,
	WorkingColorSpace targetColorSpace = WorkingColorSpace.Default
)

Parameters

bitmap  SKBitmap
The original bitmap to convert.
quantizer  IQuantizer
An optional IQuantizer instance to determine the colors of the result. Can be to pick a quantizer automatically that matches the other parameters. If no further parameters are specified, then the original pixel format is preserved while colors are optionally quantized.
ditherer  IDitherer  (Optional)
The ditherer to be used. Might be ignored if quantizer is not specified and colorType represents a higher bits-per-pixel per color channel format. This parameter is optional.
Default value: .
colorType  SKColorType  (Optional)
Determines the ColorType property of the result SKBitmap. Can be Unknown to use the original color type of the source bitmap. This parameter is optional.
Default value: Unknown.
alphaType  SKAlphaType  (Optional)
Determines the AlphaType property of the result SKBitmap. It might be ignored if the colorType cannot have the specified alpha type. Can be Unknown to use the original alpha type of the source bitmap. This parameter is optional.
Default value: Unknown.
targetColorSpace  WorkingColorSpace  (Optional)
Determines both the ColorSpace property of the result SKBitmap, and also the working color space if quantizer is . Can be Default to preserve the original color space. This parameter is optional.
Default value: Default.

Return Value

SKBitmap
A new SKBitmap instance with the desired parameters.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type SKBitmap. 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 ConvertPixelFormatAsync(SKBitmap, SKColorType, SKAlphaType, WorkingColorSpace, SKColor, Byte, TaskConfig) method for asynchronous call and to adjust parallelization, set up cancellation and for reporting progress.
  • The targetColorSpace parameter is purposely not an SKColorSpace value because only sRGB and linear color spaces are supported directly. If its value is Linear, then both the actual color space of the result and the working color space of the conversion operation will be in the linear color space (unless quantizer is specified, which determines the working color space). To create a result with sRGB color space but perform the conversion in the linear color space you can use a quantizer and configure it to work in the linear color space.

If the result SKBitmap can represent fewer colors than the source bitmap and quantizer is , then a default quantization will occur during the conversion. To use a quantizer with a specific palette you can use the PredefinedColorsQuantizer class.

If only the quantizer parameter is specified, then the original pixel format will be preserved but the actual colors will be quantized. You can also specify the ditherer parameter to preserve more details while reducing the colors.

Using a quantizer that can represent more colors than the result SKBitmap may end up in a poor quality result.

Example

The method may produce the following results:

Original image
Quantized image

Color hues with alpha gradient
Color hues with alpha gradient

Color hues with unpremultiplied ARGB4444 sRGB pixel format
Converting without a quantizer and ditherer, colorType = Argb4444, alphaType = Unpremul.

Color hues with unpremultiplied ARGB4444 sRGB pixel format dithered by Bayer 8x8 dithering and black back color.
colorType = Argb4444, using Bayer 8x8 dithering. No quantizer is specified so a default black back color was applied. As this dithering does not support partial transparency, the alpha pixels were blended with black and the bottom 16 lines are fully transparent.

Color hues quantized to ARGB1555 color space with white background and dithered by dotted halftone dithering.
Using Argb1555 quantizer with white background, alpha threshold = 16 along with Dotted Halftone dithering. All of the other parameters have their default value so the result color type and alpha type are the same as the original, it's just the specified quantizer that reduces the number of colors and turns the bottom 16 lines completely transparent.

Exceptions

ArgumentNullExceptionbitmap is .
ArgumentOutOfRangeExceptioncolorType, alphaType or targetColorSpace does not specify a defined value.

See Also