dotnet/wpf · error · InvalidOperationException

SR.Image_IndexedPixelFormatRequiresPalette

Error message

SR.Image_IndexedPixelFormatRequiresPalette

What it means

InitFromMemoryPtr requires that any palettized (indexed) pixel format (e.g. Indexed1/2/4/8, Gray4) is accompanied by a non-null BitmapPalette. WIC needs the palette to map pixel indices to colors; without one the bitmap cannot be interpreted, so an InvalidOperationException is thrown.

Solutions

  1. Pass a BitmapPalette (e.g. new BitmapPalette(colors) or BitmapPalettes.WebPalette) when using an indexed format
  2. Switch to a non-palettized format (Bgra32, Bgr32, Gray8) if a palette is not needed
  3. For PNG-like indexed output, build the palette from the distinct colors in your data

Example fix

// before
var bmp = new CachedBitmap(pixels, w, h, 96, 96, PixelFormats.Indexed8, null, stride);
// after
var palette = BitmapPalettes.Haltonne256;
var bmp = new CachedBitmap(pixels, w, h, 96, 96, PixelFormats.Indexed8, palette, stride);
Defensive patterns

Strategy: validation

Validate before calling

if (format.Palettized && palette == null)
    palette = BitmapPalettes.Halftone256; // or build from data

Type guard

static bool ReadyForIndexed(PixelFormat f, BitmapPalette p) => !f.Palettized || p != null;

Try / catch

try { return CreateIndexed(pixels, w, h, fmt, palette, stride); }
catch (InvalidOperationException) { return CreateIndexed(pixels, w, h, fmt, BitmapPalettes.Halftone256, stride); }

Prevention

When it happens

Trigger: Calling the CachedBitmap constructor (or InitFromMemoryPtr directly) with pixelFormat such as PixelFormats.Indexed8 or Indexed4 while passing null for the palette parameter.

Common situations: Creating indexed-color bitmaps from raw data and forgetting the palette; copying constructor defaults from non-indexed examples; assuming WIC supplies a default palette (it does not here).

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/dc54518f2d0cc870. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/CachedBitmap.cs:344

        }

        ///
        /// Create from memory
        ///
        private void InitFromMemoryPtr(
                    int pixelWidth,
                    int pixelHeight,
                    double dpiX,
                    double dpiY,
                    PixelFormat pixelFormat,
                    BitmapPalette palette,
                    IntPtr buffer,
                    int bufferSize,
                    int stride
                    )
        {
            if (pixelFormat.Palettized && palette == null)
                throw new InvalidOperationException(SR.Image_IndexedPixelFormatRequiresPalette);

            if (pixelFormat.Format == PixelFormatEnum.Default && pixelFormat.Guid == WICPixelFormatGUIDs.WICPixelFormatDontCare)
            {
                throw new System.ArgumentException(
                        SR.Format(SR.Effect_PixelFormat, pixelFormat),
                        nameof(pixelFormat)
                        );
            }

            _bitmapInit.BeginInit();

            try
            {
                BitmapSourceSafeMILHandle wicBitmap;

                // Create the unmanaged resources
                Guid guidFmt = pixelFormat.Guid;

View on GitHub (pinned to 81131a70a4)