BmpEncoder Class

Immutable settings for BMP encoding (Windows bitmaps).

public sealed class BmpEncoder : ImageEncoder

Remarks

By default the output is the most interoperable subset: uncompressed bottom-up rows padded to four bytes, either 24-bit BI_RGB in a BITMAPINFOHEADER or 32-bit BI_BITFIELDS in a BITMAPV4HEADER with an explicit alpha mask (BmpPixelLayout.Auto). BmpEncoder.PixelLayout also selects 1-, 4- and 8-bit indexed layouts and 16-bit 5-6-5 and 5-5-5 layouts, BmpEncoder.Compression run-length encodes the 4- and 8-bit indexed layouts, and BmpEncoder.RowOrder writes top-down rows. Embedded JPEG/PNG payloads and OS/2 headers are never written.

Nothing is lost silently. Gray pixel formats are written as gray RGB (R = G = B). Alpha is only preserved by BmpPixelLayout.Bgra32: every other layout requires BmpEncoder.BackgroundColor as soon as a pixel is not opaque. 16-bit pixel formats, and the 16-bit layouts, require BmpEncoder.AllowBitDepthReduction. An indexed layout stores the colors of the image exactly when its palette can hold them, and requires BmpEncoder.AllowColorReduction otherwise (see BmpEncoder.Quantization).

The indexed layouts read the frame twice (its colors, then its pixels) and keep one byte per pixel of working memory, charged to the resource limits; the other layouts keep one row.

BMP stores exactly one frame: animated images and images with a poster frame are rejected with an UnsupportedImageFeatureException; use ImageFrame.Save to save one frame explicitly.

Metadata (subject to ImageEncoder.MetadataHandling): only the resolution (biXPelsPerMeter/biYPelsPerMeter). An ICC profile, EXIF, XMP, an orientation other than ExifOrientation.TopLeft and text entries cannot be stored (rejected by default).

Inheritance
ImageEncoder BmpEncoder

Constructors

NameDescription
BmpEncoder()

BmpEncoder()

BmpEncoder.cs
public BmpEncoder()

No documentation is available.

Properties

NameDescription
AllowBitDepthReductionGets a value indicating whether samples may be reduced to the precision of the layout (nearest rounding): 16-bit pixel formats to 8 bits per component, and any pixel format to the 5 or 6 bits per component of BmpPixelLayout.Bgr565 and BmpPixelLayout.Bgr555. Defaults to false.
AllowColorReductionGets a value indicating whether an indexed layout may reduce the colors of an image whose colors its palette cannot hold exactly (more distinct colors than the smaller of QuantizeOptions.MaxColors and the capacity of the layout, or colors that are not in QuantizeOptions.Palette). Defaults to false: such an image is rejected with an UnsupportedImageFeatureException. When true, the colors are reduced as BmpEncoder.Quantization says. An image whose colors fit is always stored exactly. Ignored by the other layouts.
BackgroundColorGets the opaque background color non-opaque pixels are composited over by the layouts that discard alpha (every layout but BmpPixelLayout.Bgra32), interpreted in the encoded color space of the image (sRGB when untagged), or null to reject non-opaque pixels. Fully opaque images never need it, and BmpPixelLayout.Bgra32 ignores it.
CompressionGets the compression. Defaults to BmpCompression.None. BmpCompression.RunLength requires BmpPixelLayout.Indexed4 or BmpPixelLayout.Indexed8 and BmpRowOrder.BottomUp; saving fails with an ArgumentException otherwise.
DitheringGets the dithering applied when an indexed layout reduces colors: BmpEncoder.Quantization.QuantizeOptions.Dithering, which it sets.
FormatGets the format produced by this encoder.
MaxColorsGets the maximum number of palette entries of an indexed layout: BmpEncoder.Quantization.QuantizeOptions.MaxColors, which it sets.
PixelLayoutGets the pixel layout. Defaults to BmpPixelLayout.Auto.
QuantizationGets how an indexed layout reduces colors to its palette. Defaults to QuantizeOptions.Default: at most 256 entries, further limited to the capacity of the layout (2, 16 or 256), without dithering. Ignored by the other layouts.
RowOrderGets the order of the rows. Defaults to BmpRowOrder.BottomUp.

AllowBitDepthReduction

BmpEncoder.cs

Gets a value indicating whether samples may be reduced to the precision of the layout (nearest rounding): 16-bit pixel formats to 8 bits per component, and any pixel format to the 5 or 6 bits per component of BmpPixelLayout.Bgr565 and BmpPixelLayout.Bgr555. Defaults to false.

public bool AllowBitDepthReduction { get; init; }

Property value

bool

AllowColorReduction

BmpEncoder.cs

Gets a value indicating whether an indexed layout may reduce the colors of an image whose colors its palette cannot hold exactly (more distinct colors than the smaller of QuantizeOptions.MaxColors and the capacity of the layout, or colors that are not in QuantizeOptions.Palette). Defaults to false: such an image is rejected with an UnsupportedImageFeatureException. When true, the colors are reduced as BmpEncoder.Quantization says. An image whose colors fit is always stored exactly. Ignored by the other layouts.

public bool AllowColorReduction { get; init; }

Property value

bool

BackgroundColor

BmpEncoder.cs

Gets the opaque background color non-opaque pixels are composited over by the layouts that discard alpha (every layout but BmpPixelLayout.Bgra32), interpreted in the encoded color space of the image (sRGB when untagged), or null to reject non-opaque pixels. Fully opaque images never need it, and BmpPixelLayout.Bgra32 ignores it.

public Rgba64? BackgroundColor { get; init; }

Property value

Rgba64?

Exceptions

ArgumentException
The color is not fully opaque.

Compression

BmpEncoder.cs

Gets the compression. Defaults to BmpCompression.None. BmpCompression.RunLength requires BmpPixelLayout.Indexed4 or BmpPixelLayout.Indexed8 and BmpRowOrder.BottomUp; saving fails with an ArgumentException otherwise.

public BmpCompression Compression { get; init; }

Property value

BmpCompression

Exceptions

ArgumentOutOfRangeException
The value is not a defined BmpCompression.

Dithering

BmpEncoder.cs
  • Obsolete: Use Quantization.Dithering, which also selects other dithering modes.

Gets the dithering applied when an indexed layout reduces colors: BmpEncoder.Quantization.QuantizeOptions.Dithering, which it sets.

[Obsolete("Use Quantization.Dithering, which also selects other dithering modes.")]
public BmpDithering Dithering { get; init; }

Property value

BmpDithering

Exceptions

ArgumentOutOfRangeException
The value is not a defined BmpDithering.

Gets the format produced by this encoder.

public override ImageFormat Format { get; }

Property value

ImageFormat

MaxColors

BmpEncoder.cs
  • Obsolete: Use Quantization.MaxColors.

Gets the maximum number of palette entries of an indexed layout: BmpEncoder.Quantization.QuantizeOptions.MaxColors, which it sets.

[Obsolete("Use Quantization.MaxColors.")]
public int MaxColors { get; init; }

Property value

int

Exceptions

ArgumentOutOfRangeException
The value is not between 2 and 256.

PixelLayout

BmpEncoder.cs

Gets the pixel layout. Defaults to BmpPixelLayout.Auto.

public BmpPixelLayout PixelLayout { get; init; }

Property value

BmpPixelLayout

Exceptions

ArgumentOutOfRangeException
The value is not a defined BmpPixelLayout.

Quantization

BmpEncoder.cs

Gets how an indexed layout reduces colors to its palette. Defaults to QuantizeOptions.Default: at most 256 entries, further limited to the capacity of the layout (2, 16 or 256), without dithering. Ignored by the other layouts.

public QuantizeOptions Quantization { get; init; }

Property value

QuantizeOptions

Exceptions

ArgumentNullException
The value is null.

Remarks

The palette is the exact colors of the image when they fit, otherwise a deterministic variance-based median cut of its colors (see PaletteBuilder), with the dithering of QuantizeOptions.Dithering; the colors are only reduced with BmpEncoder.AllowColorReduction. The palette only stores the entries it has (biClrUsed).

With QuantizeOptions.Palette, the pixels map to that palette, whose entries that are not opaque are composited over BmpEncoder.BackgroundColor first. Saving fails with an ArgumentException when an entry is not opaque without BmpEncoder.BackgroundColor, or when the palette has more colors than the layout holds.

Gets the order of the rows. Defaults to BmpRowOrder.BottomUp.

public BmpRowOrder RowOrder { get; init; }

Property value

BmpRowOrder

Exceptions

ArgumentOutOfRangeException
The value is not a defined BmpRowOrder.