dotnet/wpf · error · InvalidOperationException

SR.Image_IndexedPixelFormatRequiresPalette

Error message

SR.Image_IndexedPixelFormatRequiresPalette

What it means

The WriteableBitmap constructor validates that a palettized (indexed) pixel format is accompanied by a BitmapPalette. If pixelFormat.Palettized is true and palette is null, it throws InvalidOperationException with SR.Image_IndexedPixelFormatRequiresPalette.

Solutions

  1. Pass a BitmapPalette instance matching the indexed format, e.g. new BitmapPalette(colors)
  2. Use a non-palettized format such as PixelFormats.Bgra32 or Pbgra32 instead
  3. Use BitmapPalettes.WebPalette or a predefined palette if exact colors don't matter

Example fix

// before
var wb = new WriteableBitmap(100, 100, 96, 96, PixelFormats.Indexed8, null); // throws
// after
var wb = new WriteableBitmap(100, 100, 96, 96, PixelFormats.Indexed8, BitmapPalettes.WebPalette);
Defensive patterns

Strategy: validation

Validate before calling

if (format.Palettized && palette == null)
{
    palette = BitmapPalettes.HaltonicPalette; // or a custom BitmapPalette
}

Type guard

static bool NeedsPalette(PixelFormat fmt) => fmt.Palettized;

Try / catch

try { wb = new WriteableBitmap(w, h, 96, 96, format, palette); }
catch (InvalidOperationException ex) when (ex.Message.Contains("palette"))
{
    wb = new WriteableBitmap(w, h, 96, 96, PixelFormats.Bgra32, null);
}

Prevention

When it happens

Trigger: new WriteableBitmap(width, height, dpiX, dpiY, palettizedFormat /* e.g. PixelFormats.Indexed8 or Indexed4 */, null).

Common situations: Passing PixelFormats.Indexed8/Indexed4/Indexed1 for indexed color output but omitting the palette argument.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs:71

        public WriteableBitmap(
            int pixelWidth,
            int pixelHeight,
            double dpiX,
            double dpiY,
            PixelFormat pixelFormat,
            BitmapPalette palette
            )
            : base(true) // Use base class virtuals
        {
            BeginInit();

            //
            // Sanitize inputs
            //

            if (pixelFormat.Palettized && palette == null)
            {
                throw new InvalidOperationException(SR.Image_IndexedPixelFormatRequiresPalette);
            }

            if (pixelFormat.Format == PixelFormatEnum.Extended)
            {
                // We don't support third-party pixel formats yet.
                throw new ArgumentException(SR.Effect_PixelFormat, nameof(pixelFormat));
            }

            if (pixelWidth < 0)
            {
                // Backwards Compat
                HRESULT.Check((int)WinCodecErrors.WINCODEC_ERR_VALUEOVERFLOW);
            }

            if (pixelWidth == 0)
            {
                // Backwards Compat
                HRESULT.Check(MS.Win32.NativeMethods.E_INVALIDARG);

View on GitHub (pinned to 81131a70a4)