{"record":{"id":"dc54518f2d0cc870","repo":"dotnet/wpf","slug":"sr-image-indexedpixelformatrequirespalette","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/CachedBitmap.cs","lineNumber":344,"sourceCode":"        }\n\n        ///\n        /// Create from memory\n        ///\n        private void InitFromMemoryPtr(\n                    int pixelWidth,\n                    int pixelHeight,\n                    double dpiX,\n                    double dpiY,\n                    PixelFormat pixelFormat,\n                    BitmapPalette palette,\n                    IntPtr buffer,\n                    int bufferSize,\n                    int stride\n                    )\n        {\n            if (pixelFormat.Palettized && palette == null)\n                throw new InvalidOperationException(SR.Image_IndexedPixelFormatRequiresPalette);\n\n            if (pixelFormat.Format == PixelFormatEnum.Default && pixelFormat.Guid == WICPixelFormatGUIDs.WICPixelFormatDontCare)\n            {\n                throw new System.ArgumentException(\n                        SR.Format(SR.Effect_PixelFormat, pixelFormat),\n                        nameof(pixelFormat)\n                        );\n            }\n\n            _bitmapInit.BeginInit();\n\n            try\n            {\n                BitmapSourceSafeMILHandle wicBitmap;\n\n                // Create the unmanaged resources\n                Guid guidFmt = pixelFormat.Guid;\n","sourceCodeStart":326,"sourceCodeEnd":362,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/CachedBitmap.cs#L326-L362","documentation":"InitFromMemoryPtr requires that any palettized (indexed) pixel format (e.g. Indexed1/2/4/8, Gray4) is accompanied by a non-null BitmapPalette. WIC needs the palette to map pixel indices to colors; without one the bitmap cannot be interpreted, so an InvalidOperationException is thrown.","triggerScenarios":"Calling the CachedBitmap constructor (or InitFromMemoryPtr directly) with pixelFormat such as PixelFormats.Indexed8 or Indexed4 while passing null for the palette parameter.","commonSituations":"Creating indexed-color bitmaps from raw data and forgetting the palette; copying constructor defaults from non-indexed examples; assuming WIC supplies a default palette (it does not here).","solutions":["Pass a BitmapPalette (e.g. new BitmapPalette(colors) or BitmapPalettes.WebPalette) when using an indexed format","Switch to a non-palettized format (Bgra32, Bgr32, Gray8) if a palette is not needed","For PNG-like indexed output, build the palette from the distinct colors in your data"],"exampleFix":"// before\nvar bmp = new CachedBitmap(pixels, w, h, 96, 96, PixelFormats.Indexed8, null, stride);\n// after\nvar palette = BitmapPalettes.Haltonne256;\nvar bmp = new CachedBitmap(pixels, w, h, 96, 96, PixelFormats.Indexed8, palette, stride);","handlingStrategy":"validation","validationCode":"if (format.Palettized && palette == null)\n    palette = BitmapPalettes.Halftone256; // or build from data","typeGuard":"static bool ReadyForIndexed(PixelFormat f, BitmapPalette p) => !f.Palettized || p != null;","tryCatchPattern":"try { return CreateIndexed(pixels, w, h, fmt, palette, stride); }\ncatch (InvalidOperationException) { return CreateIndexed(pixels, w, h, fmt, BitmapPalettes.Halftone256, stride); }","preventionTips":["Check PixelFormat.Palettized before any bitmap construction","Keep a default palette constant handy for indexed formats","Quantize colors and build a palette from the data when exact colors matter"],"tags":["wpf","imaging","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"}