{"record":{"id":"29cc35445daba240","repo":"dotnet/wpf","slug":"sr-color-nullcolorcontext","errorCode":null,"errorMessage":"SR.Color_NullColorContext","messagePattern":"SR\\.Color_NullColorContext","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/ColorConvertedBitmap.cs","lineNumber":147,"sourceCode":"            }\n        }\n\n        internal override bool IsValidForFinalizeCreation(bool throwIfInvalid)\n        {\n            if (Source == null)\n            {\n                if (throwIfInvalid)\n                {\n                    throw new InvalidOperationException(SR.Format(SR.Image_NoArgument, \"Source\"));\n                }\n                return false;\n            }\n\n            if (SourceColorContext == null)\n            {\n                if (throwIfInvalid)\n                {\n                    throw new InvalidOperationException(SR.Color_NullColorContext);\n                }\n                return false;\n            }\n\n            if (DestinationColorContext == null)\n            {\n                if (throwIfInvalid)\n                {\n                    throw new InvalidOperationException(SR.Format(SR.Image_NoArgument, \"DestinationColorContext\"));\n                }\n                return false;\n            }\n\n            return true;\n}\n\n        /// <summary>\n        ///     Notification on source colorcontext changing.","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/ColorConvertedBitmap.cs#L129-L165","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set SourceColorContext to a valid ColorContext (e.g. new ColorContext(PixelFormats.Bgra32) or loaded from an ICC profile) before EndInit","If the source has embedded context, copy it: source.ColorContexts[0]","Ensure both SourceColorContext and DestinationColorContext are non-null"],"exampleFix":"// before\nccb.BeginInit();\nccb.Source = src;\nccb.DestinationColorContext = dstCtx;\nccb.EndInit(); // throws\n// after\nccb.BeginInit();\nccb.Source = src;\nccb.SourceColorContext = src.ColorContexts != null && src.ColorContexts.Count > 0 ? src.ColorContexts[0] : new ColorContext(PixelFormats.Bgra32);\nccb.DestinationColorContext = dstCtx;\nccb.EndInit();","handlingStrategy":"validation","validationCode":"if (ccb.SourceColorContext == null)\n    ccb.SourceColorContext = src.ColorContexts is IList<ColorContext> ctxs && ctxs.Count > 0 ? ctxs[0] : new ColorContext(PixelFormats.Bgra32);","typeGuard":"static bool HasSourceContext(BitmapSource s) => s?.ColorContexts != null && s.ColorContexts.Count > 0;","tryCatchPattern":"try { ccb.EndInit(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"ColorContext\") || ex.Message.Contains(\"context\")) { ccb.SourceColorContext = new ColorContext(PixelFormats.Bgra32); ccb.EndInit(); }","preventionTips":["Always supply both contexts explicitly for ColorConvertedBitmap","Reuse the source bitmap's embedded ColorContexts[0] when available","Load ICC profiles once and cache the ColorContext instances"],"tags":["wpf","imaging","color-context"],"backgroundTag":"missing-required-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}