dotnet/wpf · error · ArgumentNullException

new ArgumentNullException(nameof(destinationProfile))

Error message

new ArgumentNullException(nameof(destinationProfile))

What it means

ColorTransformHelper.CreateTransform throws ArgumentNullException(nameof(destinationProfile)) when the destinationProfile handle is null or invalid. Both source and destination ICC profile handles must be live for Mscms to build the color transform.

Solutions

  1. Verify destinationProfile != null && !destinationProfile.IsInvalid before calling CreateTransform
  2. Load the destination profile from a valid ColorContext/PixelFormats standard profile first
  3. Catch ArgumentNullException and substitute the sRGB destination profile
  4. Fix the destination profile load failure (path, deployment, ICC validity)

Example fix

// before
helper.CreateTransform(srcHandle, dstHandle); // dstHandle invalid
// after
if (dstHandle == null || dstHandle.IsInvalid) { dstHandle = new ColorContext(PixelFormats.Srgb).GetProfileHandle(); } helper.CreateTransform(srcHandle, dstHandle);
Defensive patterns

Strategy: validation

Validate before calling

if (destinationProfile == null || destinationProfile.IsInvalid) throw new InvalidOperationException("Destination profile must be loaded before CreateTransform");

Type guard

bool HasValidDestination(SafeProfileHandle h) => h != null && !h.IsInvalid;

Try / catch

try { helper.CreateTransform(src, dst); } catch (ArgumentNullException) { dst = new ColorContext(PixelFormats.Srgb).ProfileHandle; helper.CreateTransform(src, dst); }

Prevention

When it happens

Trigger: Calling CreateTransform with destinationProfile null or with a handle wrapping IntPtr.Zero / a disposed profile, e.g. the destination ColorContext failed to load its profile.

Common situations: Destination profile file missing so its handle is invalid; passing default(null) destination to rely on a profile that was never created; disposed profiles due to ordering bugs.

Related errors


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

Appendix: source

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

    internal class ColorTransformHelper
    {
        /// Constructor
        internal ColorTransformHelper()
        {
        }

        /// Creates an ICM Profile Transform
        /// Retrieves a standard color space profile
        internal void CreateTransform(SafeProfileHandle sourceProfile, SafeProfileHandle destinationProfile)
        {
            if (sourceProfile == null || sourceProfile.IsInvalid)
            {
                throw new ArgumentNullException(nameof(sourceProfile));
            }

            if (destinationProfile == null || destinationProfile.IsInvalid)
            {
                throw new ArgumentNullException(nameof(destinationProfile));
            }

            IntPtr[] handles = new IntPtr[2];
            bool success = true;

            sourceProfile.DangerousAddRef(ref success);
            Debug.Assert(success);
            destinationProfile.DangerousAddRef(ref success);
            Debug.Assert(success);

            try
            {
                handles[0] = sourceProfile.DangerousGetHandle();
                handles[1] = destinationProfile.DangerousGetHandle();

                UInt32[] dwIntents = new UInt32[2] {INTENT_PERCEPTUAL, INTENT_PERCEPTUAL};

                // No need to get rid of the old handle as it will get GC'ed

View on GitHub (pinned to 81131a70a4)