dotnet/wpf · error · ArgumentException

SR.Image_PaletteFixedType

Error message

SR.Image_PaletteFixedType

What it means

BitmapPalette's predefined-palette constructor validates the WICPaletteType and throws ArgumentException with SR.Image_PaletteFixedType when the type is not one of the supported fixed palette types (e.g. WICPaletteTypeFixedBW, FixedHalftone8/16/64/216/252/256, FixedGray1/2/4/16/256). Any other WICPaletteType value is rejected.

Solutions

  1. Use one of the supported WICPaletteTypeFixed* values, or use the BitmapPalettes static properties instead.
  2. Cast only verified fixed-palette enum values when bridging from native WIC code.
  3. Prefer the public BitmapPalette(IList<Color>) constructor over the fixed-type path.

Example fix

// before
new BitmapPalette(WICPaletteType.WICPaletteTypeCustom); // throws
// after
new BitmapPalette(WICPaletteType.WICPaletteTypeFixedHalftone256);
Defensive patterns

Strategy: validation

Validate before calling

bool IsFixedPaletteType(WICPaletteType t) =>
    t is WICPaletteType.WICPaletteTypeFixedBW or WICPaletteType.WICPaletteTypeFixedHalftone8
      or WICPaletteType.WICPaletteTypeFixedHalftone16 or WICPaletteType.WICPaletteTypeFixedHalftone64
      or WICPaletteType.WICPaletteTypeFixedHalftone216 or WICPaletteType.WICPaletteTypeFixedHalftone252
      or WICPaletteType.WICPaletteTypeFixedHalftone256 or WICPaletteType.WICPaletteTypeFixedGray1
      or WICPaletteType.WICPaletteTypeFixedGray2 or WICPaletteType.WICPaletteTypeFixedGray4
      or WICPaletteType.WICPaletteTypeFixedGray16 or WICPaletteType.WICPaletteTypeFixedGray256;

Type guard

bool IsValidEnum<T>(object v) where T : struct, Enum => Enum.IsDefined(typeof(T), v);

Try / catch

try { var p = new BitmapPalette(paletteType); } catch (ArgumentException ex) { /* fall back to explicit color list */ }

Prevention

When it happens

Trigger: Calling the internal/predefined BitmapPalette constructor with a WICPaletteType outside the fixed set, such as WICPaletteTypeCustom or an arbitrary casted integer.

Common situations: Interop code passing raw WIC palette types into managed wrappers; typos or wrong enum members when creating predefined palettes; version drift where a WIC enum value exists in native code but is unsupported by the managed switch.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapPalette.cs:115

                bool addtransparentColor)
        {
            switch (paletteType)
            {
                case WICPaletteType.WICPaletteTypeFixedBW:
                case WICPaletteType.WICPaletteTypeFixedHalftone8:
                case WICPaletteType.WICPaletteTypeFixedHalftone27:
                case WICPaletteType.WICPaletteTypeFixedHalftone64:
                case WICPaletteType.WICPaletteTypeFixedHalftone125:
                case WICPaletteType.WICPaletteTypeFixedHalftone216:
                case WICPaletteType.WICPaletteTypeFixedHalftone252:
                case WICPaletteType.WICPaletteTypeFixedHalftone256:
                case WICPaletteType.WICPaletteTypeFixedGray4:
                case WICPaletteType.WICPaletteTypeFixedGray16:
                case WICPaletteType.WICPaletteTypeFixedGray256:
                    break;

                default:
                    throw new System.ArgumentException(SR.Format(SR.Image_PaletteFixedType, paletteType));
            }

            _palette = CreateInternalPalette();

            HRESULT.Check(UnsafeNativeMethods.WICPalette.InitializePredefined(
                        _palette,
                        paletteType,
                        addtransparentColor));

            // Fill in the Colors property.
            UpdateManaged();
        }

        internal BitmapPalette(SafeMILHandle unmanagedPalette)
        {
            _palette = unmanagedPalette;

            // Fill in the Colors property.

View on GitHub (pinned to 81131a70a4)