{"record":{"id":"55c4fbc894f0ed1f","repo":"dotnet/wpf","slug":"sr-image-palettezerocolors","errorCode":null,"errorMessage":"SR.Image_PaletteZeroColors","messagePattern":"SR\\.Image_PaletteZeroColors","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapPalette.cs","lineNumber":37,"sourceCode":"        /// <summary>\n        /// No public default constructor\n        /// </summary>\n        private BitmapPalette()\n        {\n        }\n\n        /// <summary>\n        /// Create a palette from the list of colors.\n        /// </summary>\n        public BitmapPalette(IList<Color> colors)\n        {\n            ArgumentNullException.ThrowIfNull(colors);\n\n            int count = colors.Count;\n\n            if (count < 1 || count > 256)\n            {\n                throw new InvalidOperationException(SR.Format(SR.Image_PaletteZeroColors, null));\n            }\n\n            Color[] colorArray = new Color[count];\n\n            for (int i = 0; i < count; ++i)\n            {\n                colorArray[i] = colors[i];\n            }\n\n            _colors = new ReadOnlyCollection<Color>(colorArray);\n\n            _palette = CreateInternalPalette();\n\n            UpdateUnmanaged();\n        }\n\n        /// <summary>\n        /// Construct BitmapPalette from a BitmapSource.","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapPalette.cs#L19-L55","documentation":"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.","triggerScenarios":"new BitmapPalette(new Color[0]) or passing an empty IList<Color>; also the internal UpdateManaged path when WIC reports 0 colors.","commonSituations":"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.","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)."],"exampleFix":"// before\nvar palette = new BitmapPalette(collectedColors); // empty\n// after\nif (collectedColors.Count > 0)\n    var palette = new BitmapPalette(collectedColors);\nelse\n    var palette = BitmapPalettes.Gray256;","handlingStrategy":"validation","validationCode":"if (colors == null || colors.Count < 1 || colors.Count > 256)\n    throw new ArgumentOutOfRangeException(nameof(colors), \"palette must have 1-256 colors\");","typeGuard":"bool IsValidPalette(IList<Color> colors) => colors != null && colors.Count >= 1 && colors.Count <= 256;","tryCatchPattern":"try { var p = new BitmapPalette(colors); } catch (InvalidOperationException) { p = BitmapPalettes.WebPalette; }","preventionTips":["Check Count is 1..256 before constructing","Use BitmapPalettes predefined palettes when unsure","Only build palettes for paletted pixel formats"],"tags":["wpf","imaging","palette","empty-collection"],"backgroundTag":"value-out-of-range","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"}