{"record":{"id":"91bf5c05d0c3f777","repo":"dotnet/wpf","slug":"sr-image-indexedpixelformatrequirespalette-91bf5c","errorCode":null,"errorMessage":"SR.Image_IndexedPixelFormatRequiresPalette","messagePattern":"SR\\.Image_IndexedPixelFormatRequiresPalette","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/FormatConvertedBitmap.cs","lineNumber":179,"sourceCode":"        }\n\n        internal override bool IsValidForFinalizeCreation(bool throwIfInvalid)\n        {\n            if (Source == null)\n            {\n                if (throwIfInvalid)\n                {\n                    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.","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/FormatConvertedBitmap.cs#L161-L197","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Supply a DestinationPalette: call BeginInit, set Source, DestinationFormat and DestinationPalette, then EndInit.","Use BitmapPalettes.WebPalette (or a custom BitmapPalette) that suits the target indexed format.","If no palette reduction is intended, choose a non-palettized DestinationFormat such as PixelFormats.Bgr24 or Bgra32."],"exampleFix":"// before\nvar fcb = new FormatConvertedBitmap();\nfcb.BeginInit();\nfcb.Source = src;\nfcb.DestinationFormat = PixelFormats.Indexed8Colors;\nfcb.EndInit(); // throws\n\n// after\nvar fcb = new FormatConvertedBitmap();\nfcb.BeginInit();\nfcb.Source = src;\nfcb.DestinationFormat = PixelFormats.Indexed8Colors;\nfcb.DestinationPalette = BitmapPalettes.WebPalette;\nfcb.EndInit();","handlingStrategy":"validation","validationCode":"bool needsPalette = format.Palettized;\nif (needsPalette && fcb.DestinationPalette == null)\n    fcb.DestinationPalette = BitmapPalettes.WebPalette;","typeGuard":"static bool HasValidPalette(PixelFormat f, BitmapPalette p) => !f.Palettized || p != null;","tryCatchPattern":"try { fcb.EndInit(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"palette\")) { fcb.DestinationPalette = BitmapPalettes.WebPalette; fcb.EndInit(); }","preventionTips":["Always pair an indexed DestinationFormat with an explicit DestinationPalette.","Prefer BitmapPalettes presets over hand-built palettes for common targets.","Initialize FormatConvertedBitmap inside BeginInit/EndInit with all properties set before EndInit."],"tags":["wpf","imaging","invalid-state-transition","bitmap"],"backgroundTag":"missing-required-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}