dotnet/wpf · error · InvalidOperationException

SR.Color_NullColorContext

Error message

SR.Color_NullColorContext

What it means

ColorConvertedBitmap needs both the source and destination color contexts to perform the conversion. When SourceColorContext is null at EndInit time, it throws an InvalidOperationException (Color_NullColorContext) since there is no source ICC context to convert from.

Solutions

  1. Set SourceColorContext to a valid ColorContext (e.g. new ColorContext(PixelFormats.Bgra32) or loaded from an ICC profile) before EndInit
  2. If the source has embedded context, copy it: source.ColorContexts[0]
  3. Ensure both SourceColorContext and DestinationColorContext are non-null

Example fix

// before
ccb.BeginInit();
ccb.Source = src;
ccb.DestinationColorContext = dstCtx;
ccb.EndInit(); // throws
// after
ccb.BeginInit();
ccb.Source = src;
ccb.SourceColorContext = src.ColorContexts != null && src.ColorContexts.Count > 0 ? src.ColorContexts[0] : new ColorContext(PixelFormats.Bgra32);
ccb.DestinationColorContext = dstCtx;
ccb.EndInit();
Defensive patterns

Strategy: validation

Validate before calling

if (ccb.SourceColorContext == null)
    ccb.SourceColorContext = src.ColorContexts is IList<ColorContext> ctxs && ctxs.Count > 0 ? ctxs[0] : new ColorContext(PixelFormats.Bgra32);

Type guard

static bool HasSourceContext(BitmapSource s) => s?.ColorContexts != null && s.ColorContexts.Count > 0;

Try / catch

try { ccb.EndInit(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("ColorContext") || ex.Message.Contains("context")) { ccb.SourceColorContext = new ColorContext(PixelFormats.Bgra32); ccb.EndInit(); }

Prevention

When it happens

Trigger: BeginInit/EndInit usage where Source and DestinationColorContext are set but SourceColorContext is omitted; constructing with the 4-arg path is fine, but the deferred path validates both contexts.

Common situations: Assuming the source context is inferred from the source bitmap (it is not for this API); omitting the context in XAML; contexts loaded asynchronously and still null at EndInit.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/29cc35445daba240. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/ColorConvertedBitmap.cs:147

            }
        }

        internal override bool IsValidForFinalizeCreation(bool throwIfInvalid)
        {
            if (Source == null)
            {
                if (throwIfInvalid)
                {
                    throw new InvalidOperationException(SR.Format(SR.Image_NoArgument, "Source"));
                }
                return false;
            }

            if (SourceColorContext == null)
            {
                if (throwIfInvalid)
                {
                    throw new InvalidOperationException(SR.Color_NullColorContext);
                }
                return false;
            }

            if (DestinationColorContext == null)
            {
                if (throwIfInvalid)
                {
                    throw new InvalidOperationException(SR.Format(SR.Image_NoArgument, "DestinationColorContext"));
                }
                return false;
            }

            return true;
}

        /// <summary>
        ///     Notification on source colorcontext changing.

View on GitHub (pinned to 81131a70a4)