{"record":{"id":"c75125266e2858ac","repo":"dotnet/wpf","slug":"sr-image-colorcontextinvalid","errorCode":null,"errorMessage":"SR.Image_ColorContextInvalid","messagePattern":"SR\\.Image_ColorContextInvalid","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ColorContextHelper.cs","lineNumber":73,"sourceCode":"            // No need to get rid of the old handle as it will get GC'ed\n            _profileHandle = UnsafeNativeMethodsMilCoreApi.Mscms.OpenColorProfile(\n                ref profile,\n                NativeMethods.PROFILE_READ,     // DesiredAccess\n                NativeMethods.FILE_SHARE_READ,  // ShareMode\n                NativeMethods.OPEN_EXISTING     // CreationMode\n                );\n        }\n\n        /// Retrieves the profile header\n        ///\n        ///\n        /// NOTE: This may fail. It is up to the caller to handle it by checking the bool.\n        /// \n        internal bool GetColorProfileHeader(out UnsafeNativeMethods.PROFILEHEADER header)\n        {\n            if (IsInvalid)\n            {\n                throw new InvalidOperationException(SR.Image_ColorContextInvalid);\n            }\n\n            return UnsafeNativeMethodsMilCoreApi.Mscms.GetColorProfileHeader(_profileHandle, out header);\n        }\n\n        /// Retrieves the color profile from handle\n        internal void GetColorProfileFromHandle(byte[] buffer, ref uint bufferSize)\n        {\n            Invariant.Assert(buffer == null || bufferSize <= buffer.Length);\n            \n            if (IsInvalid)\n            {\n                throw new InvalidOperationException(SR.Image_ColorContextInvalid);\n            }\n\n            // If the buffer is null, this function will return FALSE because it didn't actually copy anything. That's fine and that's\n            // what we want.\n            if (!UnsafeNativeMethodsMilCoreApi.Mscms.GetColorProfileFromHandle(_profileHandle, buffer, ref bufferSize) && buffer != null)","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ColorContextHelper.cs#L55-L91","documentation":"SafeProfileHandle.GetColorProfileHeader throws InvalidOperationException(SR.Image_ColorContextInvalid) when the underlying ICC profile handle is invalid (closed or never initialized). GetColorProfileHeader requires a live Mscms profile handle to read the PROFILEHEADER.","triggerScenarios":"Calling GetColorProfileHeader on a ColorContextHelper/SafeProfileHandle whose _profileHandle is IntPtr.Zero or has been disposed, e.g. after the ColorContext was disposed or failed to load a profile.","commonSituations":"Using a ColorContext after disposal; a ColorContext constructed from a stream that failed to produce a profile handle; double-dispose/finalizer races in imaging pipelines.","solutions":["Check IsInvalid (or that the ColorContext loaded successfully) before calling GetColorProfileHeader","Ensure the source stream/file actually contained a valid ICC profile so the handle was created","Keep the ColorContext alive while reading the header; do not dispose early","Catch InvalidOperationException and fall back to a default sRGB header"],"exampleFix":"// before\nhelper.GetColorProfileHeader(out var header);\n// after\nif (!helper.IsInvalid && helper.GetColorProfileHeader(out var header)) { /* use header */ }","handlingStrategy":"type-guard","validationCode":"if (helper == null || helper.IsInvalid) throw new InvalidOperationException(\"Color profile handle is not initialized\");","typeGuard":"bool CanReadHeader(SafeProfileHandle h) => h != null && !h.IsInvalid;","tryCatchPattern":"try { ok = helper.GetColorProfileHeader(out header); } catch (InvalidOperationException) { ok = false; }","preventionTips":["Check IsInvalid before any profile operation","Keep the owning ColorContext alive during use","Validate profiles loaded successfully at construction time"],"tags":["wpf","icc","invalid-handle"],"backgroundTag":"invalid-state-transition","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"}