dotnet/wpf · error · InvalidOperationException
SR.Image_PaletteZeroColors
Error message
SR.Image_PaletteZeroColors
What it means
BitmapPalette's constructor requires the colors collection to contain between 1 and 256 entries; an empty (or invalid-count) collection throws InvalidOperationException with SR.Image_PaletteZeroColors. A palette must map at least one color and at most 256 (8-bit index limit) colors.
Solutions
- Check colors.Count is between 1 and 256 before constructing the BitmapPalette.
- Provide a default palette (e.g. BitmapPalettes.WebPalette) when no colors were extracted.
- Skip palette creation entirely for non-paletted pixel formats (PalettColor formats only).
Example fix
// before
var palette = new BitmapPalette(collectedColors); // empty
// after
if (collectedColors.Count > 0)
var palette = new BitmapPalette(collectedColors);
else
var palette = BitmapPalettes.Gray256; Defensive patterns
Strategy: validation
Validate before calling
if (colors == null || colors.Count < 1 || colors.Count > 256)
throw new ArgumentOutOfRangeException(nameof(colors), "palette must have 1-256 colors"); Type guard
bool IsValidPalette(IList<Color> colors) => colors != null && colors.Count >= 1 && colors.Count <= 256;
Try / catch
try { var p = new BitmapPalette(colors); } catch (InvalidOperationException) { p = BitmapPalettes.WebPalette; } Prevention
- Check Count is 1..256 before constructing
- Use BitmapPalettes predefined palettes when unsure
- Only build palettes for paletted pixel formats
When it happens
Trigger: new BitmapPalette(new Color[0]) or passing an empty IList<Color>; also the internal UpdateManaged path when WIC reports 0 colors.
Common situations: Building a palette dynamically from image data that yielded no colors; decoding a malformed paletted image where the color table is empty; default-constructed color lists passed straight to the constructor.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- SR.Image_IndexedPixelFormatRequiresPalette
- SR.Image_PaletteColorsDoNotMatchFormat
- SR.Image_PaletteFixedType
- Image_EncoderNoColorContext
- Image_EncoderNoGlobalMetadata
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/55c4fbc894f0ed1f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapPalette.cs:37
/// <summary>
/// No public default constructor
/// </summary>
private BitmapPalette()
{
}
/// <summary>
/// Create a palette from the list of colors.
/// </summary>
public BitmapPalette(IList<Color> colors)
{
ArgumentNullException.ThrowIfNull(colors);
int count = colors.Count;
if (count < 1 || count > 256)
{
throw new InvalidOperationException(SR.Format(SR.Image_PaletteZeroColors, null));
}
Color[] colorArray = new Color[count];
for (int i = 0; i < count; ++i)
{
colorArray[i] = colors[i];
}
_colors = new ReadOnlyCollection<Color>(colorArray);
_palette = CreateInternalPalette();
UpdateUnmanaged();
}
/// <summary>
/// Construct BitmapPalette from a BitmapSource.View on GitHub (pinned to 81131a70a4)