dotnet/wpf · error · ArgumentNullException

new ArgumentNullException(nameof(sourceProfile))

Error message

new ArgumentNullException(nameof(sourceProfile))

What it means

ColorTransformHelper.CreateTransform throws ArgumentNullException(nameof(sourceProfile)) when the sourceProfile handle is null or invalid. A valid, open source ICC profile handle is required to create the Mscms color transform.

Solutions

  1. Ensure the source profile loaded successfully (e.g. from a valid ColorContext) before calling CreateTransform
  2. Check sourceProfile != null && !sourceProfile.IsInvalid before the call
  3. Catch ArgumentNullException and fall back to a default sRGB source profile
  4. Fix the root cause of the failed profile load (missing file, invalid ICC data)

Example fix

// before
helper.CreateTransform(srcHandle, dstHandle); // srcHandle may be invalid
// after
if (srcHandle == null || srcHandle.IsInvalid) { srcHandle = ColorContexts.GetStandardProfileHandle(PixelFormats.Srgb); } helper.CreateTransform(srcHandle, dstHandle);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

try { helper.CreateTransform(src, dst); } catch (ArgumentNullException) { src = GetDefaultSrgbProfile(); helper.CreateTransform(src, dst); }

Prevention

When it happens

Trigger: Calling CreateTransform with a null SafeProfileHandle or one wrapping an invalid/zero handle, e.g. when the source profile failed to load (bad URI, corrupt ICC data).

Common situations: A ColorContext whose profile never loaded producing an invalid handle passed straight into CreateTransform; forgetting to construct the source profile before creating the transform.

Related errors


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

Appendix: source

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

    #region ColorTransformHelper

    /// <summary>
    /// Class to call into MSCMS color transform APIs
    /// </summary>
    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();

View on GitHub (pinned to 81131a70a4)