{"record":{"id":"2f02780bcd449143","repo":"dotnet/wpf","slug":"sr-image-colortransforminvalid","errorCode":null,"errorMessage":"SR.Image_ColorTransformInvalid","messagePattern":"SR\\.Image_ColorTransformInvalid","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ColorTransformHelper.cs","lineNumber":110,"sourceCode":"            finally\n            {\n                sourceProfile.DangerousRelease();\n                destinationProfile.DangerousRelease();\n            }\n\n            if (_transformHandle == null || _transformHandle.IsInvalid)\n            {\n                HRESULT.Check(Marshal.GetHRForLastWin32Error());\n            }\n        }\n\n        /// Translates the colors\n        /// Retrieves a standard color space profile\n        internal void TranslateColors(IntPtr paInputColors, UInt32 numColors, UInt32 inputColorType, IntPtr paOutputColors, UInt32 outputColorType)\n        {\n            if (_transformHandle == null || _transformHandle.IsInvalid)\n            {\n                throw new InvalidOperationException(SR.Image_ColorTransformInvalid);\n            }\n\n            HRESULT.Check(UnsafeNativeMethods.Mscms.TranslateColors(\n                _transformHandle,\n                paInputColors,\n                numColors,\n                inputColorType,\n                paOutputColors,\n                outputColorType));\n        }\n\n\n        #region Data members\n\n        /// Handle for the ICM Color Transform\n        private ColorTransformHandle _transformHandle;\n\n        /// Intents","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ColorTransformHelper.cs#L92-L128","documentation":"ColorTransformHelper.TranslateColors throws InvalidOperationException(SR.Image_ColorTransformInvalid) when the internal Mscms transform handle is null or invalid. Colors can only be translated through a successfully created ICM transform; a missing handle means CreateTransform never ran or the transform was disposed.","triggerScenarios":"Calling TranslateColors on a ColorTransformHelper whose _transformHandle is invalid — CreateTransform was not called, failed, or the transform was disposed/finalized before translation.","commonSituations":"Skipping the CreateTransform setup step; disposing the ColorTransform then reusing it; exceptions during CreateTransform swallowed upstream leaving the handle uninitialized.","solutions":["Call CreateTransform successfully before TranslateColors and check for errors","Verify _transformHandle (or the public transform object) is valid before translating","Keep the ColorTransform alive; do not dispose between creation and color translation","Catch InvalidOperationException and lazily re-create the transform on demand"],"exampleFix":"// before\nhelper.TranslateColors(inputPtr, count, inType, outPtr, outType);\n// after\nif (helper._transformHandle == null || helper._transformHandle.IsInvalid) { helper.CreateTransform(src, dst); } helper.TranslateColors(inputPtr, count, inType, outPtr, outType);","handlingStrategy":"try-catch","validationCode":"if (transform == null || transform.TransformHandle == null || transform.TransformHandle.IsInvalid) { transform = ColorTransformHelper.Create(srcProfile, dstProfile); }","typeGuard":"bool CanTranslate(ColorTransformHelper t) => t != null && t._transformHandle != null && !t._transformHandle.IsInvalid;","tryCatchPattern":"try { helper.TranslateColors(inPtr, n, inType, outPtr, outType); } catch (InvalidOperationException) { helper.CreateTransform(src, dst); helper.TranslateColors(inPtr, n, inType, outPtr, outType); }","preventionTips":["Always complete CreateTransform before TranslateColors","Keep the transform object alive across the translation batch","Lazily re-create the transform if it was disposed"],"tags":["wpf","color-management","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-22T01:17:13.364Z"}