{"record":{"id":"b667fc4cb4dcdeea","repo":"dotnet/wpf","slug":"sr-image-indexedpixelformatrequirespalette-writeablebitmap","errorCode":null,"errorMessage":"SR.Image_IndexedPixelFormatRequiresPalette","messagePattern":"SR\\.Image_IndexedPixelFormatRequiresPalette","errorType":"validation","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs","lineNumber":71,"sourceCode":"        public WriteableBitmap(\n            int pixelWidth,\n            int pixelHeight,\n            double dpiX,\n            double dpiY,\n            PixelFormat pixelFormat,\n            BitmapPalette palette\n            )\n            : base(true) // Use base class virtuals\n        {\n            BeginInit();\n\n            //\n            // Sanitize inputs\n            //\n\n            if (pixelFormat.Palettized && palette == null)\n            {\n                throw new InvalidOperationException(SR.Image_IndexedPixelFormatRequiresPalette);\n            }\n\n            if (pixelFormat.Format == PixelFormatEnum.Extended)\n            {\n                // We don't support third-party pixel formats yet.\n                throw new ArgumentException(SR.Effect_PixelFormat, nameof(pixelFormat));\n            }\n\n            if (pixelWidth < 0)\n            {\n                // Backwards Compat\n                HRESULT.Check((int)WinCodecErrors.WINCODEC_ERR_VALUEOVERFLOW);\n            }\n\n            if (pixelWidth == 0)\n            {\n                // Backwards Compat\n                HRESULT.Check(MS.Win32.NativeMethods.E_INVALIDARG);","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs#L53-L89","documentation":"The WriteableBitmap constructor validates that a palettized (indexed) pixel format is accompanied by a BitmapPalette. If pixelFormat.Palettized is true and palette is null, it throws InvalidOperationException with SR.Image_IndexedPixelFormatRequiresPalette.","triggerScenarios":"new WriteableBitmap(width, height, dpiX, dpiY, palettizedFormat /* e.g. PixelFormats.Indexed8 or Indexed4 */, null).","commonSituations":"Passing PixelFormats.Indexed8/Indexed4/Indexed1 for indexed color output but omitting the palette argument.","solutions":["Pass a BitmapPalette instance matching the indexed format, e.g. new BitmapPalette(colors)","Use a non-palettized format such as PixelFormats.Bgra32 or Pbgra32 instead","Use BitmapPalettes.WebPalette or a predefined palette if exact colors don't matter"],"exampleFix":"// before\nvar wb = new WriteableBitmap(100, 100, 96, 96, PixelFormats.Indexed8, null); // throws\n// after\nvar wb = new WriteableBitmap(100, 100, 96, 96, PixelFormats.Indexed8, BitmapPalettes.WebPalette);","handlingStrategy":"validation","validationCode":"if (format.Palettized && palette == null)\n{\n    palette = BitmapPalettes.HaltonicPalette; // or a custom BitmapPalette\n}","typeGuard":"static bool NeedsPalette(PixelFormat fmt) => fmt.Palettized;","tryCatchPattern":"try { wb = new WriteableBitmap(w, h, 96, 96, format, palette); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"palette\"))\n{\n    wb = new WriteableBitmap(w, h, 96, 96, PixelFormats.Bgra32, null);\n}","preventionTips":["Check PixelFormat.Palettized before choosing null for the palette parameter","Use predefined BitmapPalettes.* for indexed formats"],"tags":["wpf","bitmap","invalidoperationexception","pixelformat","palette"],"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-22T01:17:13.364Z"}