dotnet/wpf · error · InvalidOperationException

SR.Image_IndexedPixelFormatRequiresPalette

Error message

SR.Image_IndexedPixelFormatRequiresPalette

What it means

FormatConvertedBitmap.EndInit validates the destination format during initialization. If DestinationFormat is an indexed (palettized) pixel format but DestinationPalette is null, WPF cannot map pixel indices to colors, so it throws this InvalidOperationException. The library requires an explicit palette for every indexed output format.

Solutions

  1. Supply a DestinationPalette: call BeginInit, set Source, DestinationFormat and DestinationPalette, then EndInit.
  2. Use BitmapPalettes.WebPalette (or a custom BitmapPalette) that suits the target indexed format.
  3. If no palette reduction is intended, choose a non-palettized DestinationFormat such as PixelFormats.Bgr24 or Bgra32.

Example fix

// before
var fcb = new FormatConvertedBitmap();
fcb.BeginInit();
fcb.Source = src;
fcb.DestinationFormat = PixelFormats.Indexed8Colors;
fcb.EndInit(); // throws

// after
var fcb = new FormatConvertedBitmap();
fcb.BeginInit();
fcb.Source = src;
fcb.DestinationFormat = PixelFormats.Indexed8Colors;
fcb.DestinationPalette = BitmapPalettes.WebPalette;
fcb.EndInit();
Defensive patterns

Strategy: validation

Validate before calling

bool needsPalette = format.Palettized;
if (needsPalette && fcb.DestinationPalette == null)
    fcb.DestinationPalette = BitmapPalettes.WebPalette;

Type guard

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

Try / catch

try { fcb.EndInit(); } catch (InvalidOperationException ex) when (ex.Message.Contains("palette")) { fcb.DestinationPalette = BitmapPalettes.WebPalette; fcb.EndInit(); }

Prevention

When it happens

Trigger: Calling EndInit (or implicit initialization via the FormatConvertedBitmap(BitmapSource, BitmapSource, PixelFormat, BitmapPalette) constructor chain) on a FormatConvertedBitmap whose DestinationFormat.Palettized is true (e.g. Indexed8Colors, Indexed4Colors, BlackWhite) while DestinationPalette is null or never set via BeginInit/EndInit ordering.

Common situations: Converting a photo to an 8-bit indexed format to shrink file size but forgetting to supply a BitmapPalette; setting DestinationFormat in XAML without a DestinationPalette; reusing a FormatConvertedBitmap across encodes where the palette was cleared.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/FormatConvertedBitmap.cs:179

        }

        internal override bool IsValidForFinalizeCreation(bool throwIfInvalid)
        {
            if (Source == null)
            {
                if (throwIfInvalid)
                {
                    throw new InvalidOperationException(SR.Format(SR.Image_NoArgument, "Source"));
                }
                return false;
            }
            if (DestinationFormat.Palettized)
            {
                if (DestinationPalette == null)
                {
                    if (throwIfInvalid)
                    {
                        throw new InvalidOperationException(SR.Image_IndexedPixelFormatRequiresPalette);
                    }
                    return false;
                }
                else if ((1 << DestinationFormat.BitsPerPixel) < DestinationPalette.Colors.Count)
                {
                    if (throwIfInvalid)
                    {
                        throw new InvalidOperationException(SR.Image_PaletteColorsDoNotMatchFormat);
                    }
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        ///     Notification on destination format changing.

View on GitHub (pinned to 81131a70a4)