dotnet/wpf · error · InvalidOperationException
Image_EncoderNoColorContext
Error message
Image_EncoderNoColorContext
What it means
The BitmapEncoder.ColorContexts setter rejects values when the encoder's codec does not support color contexts (_supportsColorContext is false), throwing InvalidOperationException(Image_EncoderNoColorContext). Most built-in codecs support this, but codecs lacking global color-context support cannot accept it.
Solutions
- Only set ColorContexts when encoder.CodecInfo supports color contexts
- Switch to a codec that supports color contexts (Png, Tiff, Jpeg, Wmp)
- Guard the assignment with a capability check derived from CodecInfo.DeviceManufacturer/Format capabilities
- Remove the ColorContexts assignment for unsupported formats
Example fix
// before
encoder.ColorContexts = sourceFrame.ColorContexts; // throws for Bmp
// after
if (encoder.CodecInfo != null && encoder is TiffBitmapEncoder || encoder is PngBitmapEncoder)
{
encoder.ColorContexts = sourceFrame.ColorContexts;
} Defensive patterns
Strategy: type-guard
Validate before calling
if (encoder is BmpBitmapEncoder) throw new NotSupportedException("Bmp encoder does not support color contexts"); Type guard
static bool SupportsColorContexts(BitmapEncoder e) => e is not BmpBitmapEncoder; // codecs w/o context support
Try / catch
try { encoder.ColorContexts = contexts; }
catch (InvalidOperationException) { // codec has no color-context support
// proceed without embedding color contexts
} Prevention
- Check codec capabilities before assigning optional encoder properties
- Keep a mapping of which WPF codecs support color contexts
- Avoid blanket-copying frame properties to all encoder types
- Test encoding pipelines per target format
When it happens
Trigger: Setting encoder.ColorContexts = [...] on a BitmapEncoder whose underlying container/codec does not support color contexts (checked in the setter after EnsureBuiltIn and a null check).
Common situations: Using BitmapEncoder.Create with a third-party or limited codec GUID, or copying metadata/color contexts between formats whose encoders have different capabilities (e.g. Bmp vs Tiff/Png).
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Image_EncoderNoGlobalMetadata
- Image_EncoderNoGlobalThumbnail
- Image_EncoderNoPreview
- Image_OnlyOneSave
- SR.Color_NullColorContext
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/b35ad68636065ee9.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapEncoder.cs:134
/// </summary>
public virtual ReadOnlyCollection<ColorContext> ColorContexts
{
get
{
VerifyAccess();
EnsureBuiltIn();
return _readOnlycolorContexts;
}
set
{
VerifyAccess();
EnsureBuiltIn();
ArgumentNullException.ThrowIfNull(value);
if (!_supportsColorContext)
{
throw new InvalidOperationException(SR.Image_EncoderNoColorContext);
}
_readOnlycolorContexts = value;
}
}
/// <summary>
/// Set or get the bitmap's global embedded thumbnail.
/// </summary>
public virtual BitmapSource Thumbnail
{
get
{
VerifyAccess();
EnsureBuiltIn();
return _thumbnail;
}
setView on GitHub (pinned to 81131a70a4)