dotnet/wpf · error · NotSupportedException

new NotSupportedException() // Only support color spaces…

Error message

new NotSupportedException() // Only support color spaces with 3,4,5,6,7,8 channels

What it means

ColorTransform.ICM2Color throws NotSupportedException when the source color span has fewer than 3 or more than 8 channels. The ICM color translation path only supports color spaces with 3-8 channels, mapping them into packed UInt16 channels.

Solutions

  1. Convert the source color to an RGB/CMYK (3-8 channel) format before applying the transform
  2. Use a supported pixel format (Bgra32, Rgba64, Cmyk32, etc.) when constructing colors for ColorTransform
  3. Catch NotSupportedException and handle grayscale/special formats with a dedicated path
  4. Check PixelFormat.ChannelsCount of the source format before invoking the transform

Example fix

// before
var color = transform.ValueToColor(grayFormat, span);
// after
if (span.Length < 3 || span.Length > 8) { throw new InvalidOperationException("Convert to a 3-8 channel format first"); } var color = transform.ValueToColor(format, span);
Defensive patterns

Strategy: validation

Validate before calling

if (srcValue.Length < 3 || srcValue.Length > 8) throw new InvalidOperationException($"Unsupported channel count {srcValue.Length}; convert to a 3-8 channel color space first");

Type guard

bool IsSupportedChannelCount(PixelFormat fmt) => fmt.BitsPerPixel >= 24 && GetChannels(fmt) is >= 3 and <= 8;

Try / catch

try { color = transform.ValueToColor(format, span); } catch (NotSupportedException) { color = TranslateViaIntermediateRgb(span); }

Prevention

When it happens

Trigger: Calling ColorTransform.ValueToColor (via inputColor) on a color whose pixel/color format yields srcValue outside 3..8 channels, e.g. grayscale (1-2 channel) or exotic >8-channel formats.

Common situations: Translating colors from grayscale or CMYK-extended/hi-fi profiles with unusual channel counts; custom pixel formats fed into the CMS pipeline; 16+ channel spectral profiles.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ColorTransform.cs:134

        //
        //  Private Methods
        //
        //------------------------------------------------------

        #region Private Methods

        private void InitializeICM()
        {
            _colorTransformHelper = new ColorTransformHelper();
        }

        private long ICM2Color(Span<float> srcValue)
        {
            long colorValue;

            if (srcValue.Length < 3 || srcValue.Length > 8)
            {
                throw new NotSupportedException(); // Only support color spaces with 3,4,5,6,7,8 channels
            }

            if (srcValue.Length <= 4)
            {
                Span<UInt16> channel = stackalloc UInt16[4];

                for (int i = 0; i < srcValue.Length; i++)
                {
                    if (srcValue[i] >= 1.0)// this fails for values above 1.0 and below 0.0
                    {
                        channel[i] = 0xffff;
                    }
                    else if (srcValue[i] <= 0.0)
                    {
                        channel[i] = 0x0;
                    }
                    else
                    {

View on GitHub (pinned to 81131a70a4)