dotnet/wpf · error · InvalidOperationException

SR.Image_ColorTransformInvalid

Error message

SR.Image_ColorTransformInvalid

What it means

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.

Solutions

  1. Call CreateTransform successfully before TranslateColors and check for errors
  2. Verify _transformHandle (or the public transform object) is valid before translating
  3. Keep the ColorTransform alive; do not dispose between creation and color translation
  4. Catch InvalidOperationException and lazily re-create the transform on demand

Example fix

// before
helper.TranslateColors(inputPtr, count, inType, outPtr, outType);
// after
if (helper._transformHandle == null || helper._transformHandle.IsInvalid) { helper.CreateTransform(src, dst); } helper.TranslateColors(inputPtr, count, inType, outPtr, outType);
Defensive patterns

Strategy: try-catch

Validate before calling

if (transform == null || transform.TransformHandle == null || transform.TransformHandle.IsInvalid) { transform = ColorTransformHelper.Create(srcProfile, dstProfile); }

Type guard

bool CanTranslate(ColorTransformHelper t) => t != null && t._transformHandle != null && !t._transformHandle.IsInvalid;

Try / catch

try { helper.TranslateColors(inPtr, n, inType, outPtr, outType); } catch (InvalidOperationException) { helper.CreateTransform(src, dst); helper.TranslateColors(inPtr, n, inType, outPtr, outType); }

Prevention

When it happens

Trigger: Calling TranslateColors on a ColorTransformHelper whose _transformHandle is invalid — CreateTransform was not called, failed, or the transform was disposed/finalized before translation.

Common situations: Skipping the CreateTransform setup step; disposing the ColorTransform then reusing it; exceptions during CreateTransform swallowed upstream leaving the handle uninitialized.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ColorTransformHelper.cs:110

            finally
            {
                sourceProfile.DangerousRelease();
                destinationProfile.DangerousRelease();
            }

            if (_transformHandle == null || _transformHandle.IsInvalid)
            {
                HRESULT.Check(Marshal.GetHRForLastWin32Error());
            }
        }

        /// Translates the colors
        /// Retrieves a standard color space profile
        internal void TranslateColors(IntPtr paInputColors, UInt32 numColors, UInt32 inputColorType, IntPtr paOutputColors, UInt32 outputColorType)
        {
            if (_transformHandle == null || _transformHandle.IsInvalid)
            {
                throw new InvalidOperationException(SR.Image_ColorTransformInvalid);
            }

            HRESULT.Check(UnsafeNativeMethods.Mscms.TranslateColors(
                _transformHandle,
                paInputColors,
                numColors,
                inputColorType,
                paOutputColors,
                outputColorType));
        }


        #region Data members

        /// Handle for the ICM Color Transform
        private ColorTransformHandle _transformHandle;

        /// Intents

View on GitHub (pinned to 81131a70a4)