{"record":{"id":"bc8a9a9be698bcad","repo":"dotnet/wpf","slug":"sr-image-palettefixedtype","errorCode":null,"errorMessage":"SR.Image_PaletteFixedType","messagePattern":"SR\\.Image_PaletteFixedType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapPalette.cs","lineNumber":115,"sourceCode":"                bool addtransparentColor)\n        {\n            switch (paletteType)\n            {\n                case WICPaletteType.WICPaletteTypeFixedBW:\n                case WICPaletteType.WICPaletteTypeFixedHalftone8:\n                case WICPaletteType.WICPaletteTypeFixedHalftone27:\n                case WICPaletteType.WICPaletteTypeFixedHalftone64:\n                case WICPaletteType.WICPaletteTypeFixedHalftone125:\n                case WICPaletteType.WICPaletteTypeFixedHalftone216:\n                case WICPaletteType.WICPaletteTypeFixedHalftone252:\n                case WICPaletteType.WICPaletteTypeFixedHalftone256:\n                case WICPaletteType.WICPaletteTypeFixedGray4:\n                case WICPaletteType.WICPaletteTypeFixedGray16:\n                case WICPaletteType.WICPaletteTypeFixedGray256:\n                    break;\n\n                default:\n                    throw new System.ArgumentException(SR.Format(SR.Image_PaletteFixedType, paletteType));\n            }\n\n            _palette = CreateInternalPalette();\n\n            HRESULT.Check(UnsafeNativeMethods.WICPalette.InitializePredefined(\n                        _palette,\n                        paletteType,\n                        addtransparentColor));\n\n            // Fill in the Colors property.\n            UpdateManaged();\n        }\n\n        internal BitmapPalette(SafeMILHandle unmanagedPalette)\n        {\n            _palette = unmanagedPalette;\n\n            // Fill in the Colors property.","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapPalette.cs#L97-L133","documentation":"BitmapPalette's predefined-palette constructor validates the WICPaletteType and throws ArgumentException with SR.Image_PaletteFixedType when the type is not one of the supported fixed palette types (e.g. WICPaletteTypeFixedBW, FixedHalftone8/16/64/216/252/256, FixedGray1/2/4/16/256). Any other WICPaletteType value is rejected.","triggerScenarios":"Calling the internal/predefined BitmapPalette constructor with a WICPaletteType outside the fixed set, such as WICPaletteTypeCustom or an arbitrary casted integer.","commonSituations":"Interop code passing raw WIC palette types into managed wrappers; typos or wrong enum members when creating predefined palettes; version drift where a WIC enum value exists in native code but is unsupported by the managed switch.","solutions":["Use one of the supported WICPaletteTypeFixed* values, or use the BitmapPalettes static properties instead.","Cast only verified fixed-palette enum values when bridging from native WIC code.","Prefer the public BitmapPalette(IList<Color>) constructor over the fixed-type path."],"exampleFix":"// before\nnew BitmapPalette(WICPaletteType.WICPaletteTypeCustom); // throws\n// after\nnew BitmapPalette(WICPaletteType.WICPaletteTypeFixedHalftone256);","handlingStrategy":"validation","validationCode":"bool IsFixedPaletteType(WICPaletteType t) =>\n    t is WICPaletteType.WICPaletteTypeFixedBW or WICPaletteType.WICPaletteTypeFixedHalftone8\n      or WICPaletteType.WICPaletteTypeFixedHalftone16 or WICPaletteType.WICPaletteTypeFixedHalftone64\n      or WICPaletteType.WICPaletteTypeFixedHalftone216 or WICPaletteType.WICPaletteTypeFixedHalftone252\n      or WICPaletteType.WICPaletteTypeFixedHalftone256 or WICPaletteType.WICPaletteTypeFixedGray1\n      or WICPaletteType.WICPaletteTypeFixedGray2 or WICPaletteType.WICPaletteTypeFixedGray4\n      or WICPaletteType.WICPaletteTypeFixedGray16 or WICPaletteType.WICPaletteTypeFixedGray256;","typeGuard":"bool IsValidEnum<T>(object v) where T : struct, Enum => Enum.IsDefined(typeof(T), v);","tryCatchPattern":"try { var p = new BitmapPalette(paletteType); } catch (ArgumentException ex) { /* fall back to explicit color list */ }","preventionTips":["Use BitmapPalettes static properties instead of raw WICPaletteType","Validate enum values with Enum.IsDefined before casting from native interop","Never cast arbitrary ints to WICPaletteType"],"tags":["wpf","imaging","palette","enum"],"backgroundTag":"invalid-enum-value","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"}