{"record":{"id":"66f03b0f6786c56f","repo":"dotnet/wpf","slug":"sr-image-palettecolorsdonotmatchformat","errorCode":null,"errorMessage":"SR.Image_PaletteColorsDoNotMatchFormat","messagePattern":"SR\\.Image_PaletteColorsDoNotMatchFormat","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/FormatConvertedBitmap.cs","lineNumber":187,"sourceCode":"                    throw new InvalidOperationException(SR.Format(SR.Image_NoArgument, \"Source\"));\n                }\n                return false;\n            }\n            if (DestinationFormat.Palettized)\n            {\n                if (DestinationPalette == null)\n                {\n                    if (throwIfInvalid)\n                    {\n                        throw new InvalidOperationException(SR.Image_IndexedPixelFormatRequiresPalette);\n                    }\n                    return false;\n                }\n                else if ((1 << DestinationFormat.BitsPerPixel) < DestinationPalette.Colors.Count)\n                {\n                    if (throwIfInvalid)\n                    {\n                        throw new InvalidOperationException(SR.Image_PaletteColorsDoNotMatchFormat);\n                    }\n                    return false;\n                }\n            }\n\n            return true;\n        }\n\n        /// <summary>\n        ///     Notification on destination format changing.\n        /// </summary>\n        private void DestinationFormatPropertyChangedHook(DependencyPropertyChangedEventArgs e)\n        {\n            if (!e.IsASubPropertyChange)\n            {\n                _destinationFormat = (PixelFormat)e.NewValue;\n            }\n        }","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/FormatConvertedBitmap.cs#L169-L205","documentation":"For indexed pixel formats, the palette cannot contain more colors than the bit depth can index ((1 << BitsPerPixel) must be >= Colors.Count). FormatConvertedBitmap throws this InvalidOperationException during EndInit when DestinationPalette holds more color entries than the DestinationFormat can address.","triggerScenarios":"EndInit on a FormatConvertedBitmap where e.g. DestinationFormat = Indexed1Colors (2 possible colors, 1bpp) but DestinationPalette contains 3+ entries; generally any palette whose Colors.Count exceeds 2^BitsPerPixel.","commonSituations":"Reusing a 256-color WebPalette with Indexed4Colors or Indexed1Formats; building a custom BitmapPalette from a source image's many colors and applying it to a low-bit-depth target; copy-pasted palette code without matching the bit depth.","solutions":["Trim the palette to at most (1 << DestinationFormat.BitsPerPixel) colors before assigning it.","Choose a DestinationFormat with enough bits per pixel for the palette size (e.g. Indexed8Colors for a 256-color palette).","Quantize the source image first (e.g. via an encoder or external quantizer) to produce a palette matching the target depth."],"exampleFix":"// before\nfcb.DestinationFormat = PixelFormats.Indexed4Colors; // 16 colors max\nfcb.DestinationPalette = BitmapPalettes.WebPalette; // 216+ colors -> throws\n\n// after\nfcb.DestinationFormat = PixelFormats.Indexed8Colors; // 256 colors max\nfcb.DestinationPalette = BitmapPalettes.WebPalette;","handlingStrategy":"validation","validationCode":"if (palette != null && palette.Colors.Count > (1 << format.BitsPerPixel))\n    throw new ArgumentException($\"Palette has {palette.Colors.Count} colors; {format.BitsPerPixel}bpp supports at most {1 << format.BitsPerPixel}.\");","typeGuard":"static bool PaletteFits(PixelFormat f, BitmapPalette p) => p == null || p.Colors.Count <= (1 << f.BitsPerPixel);","tryCatchPattern":"try { fcb.EndInit(); } catch (InvalidOperationException ex) { /* shrink palette or raise bpp, then retry */ }","preventionTips":["Compute max colors as 1 << BitsPerPixel and assert palette size against it before assigning.","Match palette presets to formats: WebPalette/Grayscale256 for Indexed8Colors, Halftone8 for Indexed1.","Quantize source colors before building a custom palette for low-depth formats."],"tags":["wpf","imaging","palette","value-out-of-range"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}