{"record":{"id":"245b81afa7a9ca7e","repo":"dotnet/wpf","slug":"sr-image-badpixelformat","errorCode":null,"errorMessage":"SR.Image_BadPixelFormat","messagePattern":"SR\\.Image_BadPixelFormat","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/PixelFormat.cs","lineNumber":223,"sourceCode":"                _ when pixelFormatString.Equals(\"Gray8\", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Gray8,\n                _ when pixelFormatString.Equals(\"Bgr555\", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Bgr555,\n                _ when pixelFormatString.Equals(\"Bgr565\", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Bgr565,\n                _ when pixelFormatString.Equals(\"Bgr24\", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Bgr24,\n                _ when pixelFormatString.Equals(\"Rgb24\", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Rgb24,\n                _ when pixelFormatString.Equals(\"Bgr101010\", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Bgr101010,\n                _ when pixelFormatString.Equals(\"Bgr32\", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Bgr32,\n                _ when pixelFormatString.Equals(\"Bgra32\", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Bgra32,\n                _ when pixelFormatString.Equals(\"Pbgra32\", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Pbgra32,\n                _ when pixelFormatString.Equals(\"Rgb48\", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Rgb48,\n                _ when pixelFormatString.Equals(\"Rgba64\", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Rgba64,\n                _ when pixelFormatString.Equals(\"Prgba64\", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Prgba64,\n                _ when pixelFormatString.Equals(\"Gray16\", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Gray16,\n                _ when pixelFormatString.Equals(\"Gray32Float\", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Gray32Float,\n                _ when pixelFormatString.Equals(\"Rgb128Float\", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Rgb128Float,\n                _ when pixelFormatString.Equals(\"Rgba128Float\", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Rgba128Float,\n                _ when pixelFormatString.Equals(\"Prgba128Float\", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Prgba128Float,\n                _ when pixelFormatString.Equals(\"Cmyk32\", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Cmyk32,\n                _ => throw new ArgumentException(SR.Format(SR.Image_BadPixelFormat, pixelFormatString), nameof(pixelFormatString)),\n            };\n\n            _flags = GetPixelFormatFlagsFromEnum(_format);\n            _bitsPerPixel = GetBitsPerPixelFromEnum(_format);\n            _guidFormat = GetGuidFromFormat(_format);\n        }\n\n        private static Guid GetGuidFromFormat(PixelFormatEnum format) => format switch\n        {\n            PixelFormatEnum.Default => WICPixelFormatGUIDs.WICPixelFormatDontCare,\n            PixelFormatEnum.Indexed1 => WICPixelFormatGUIDs.WICPixelFormat1bppIndexed,\n            PixelFormatEnum.Indexed2 => WICPixelFormatGUIDs.WICPixelFormat2bppIndexed,\n            PixelFormatEnum.Indexed4 => WICPixelFormatGUIDs.WICPixelFormat4bppIndexed,\n            PixelFormatEnum.Indexed8 => WICPixelFormatGUIDs.WICPixelFormat8bppIndexed,\n            PixelFormatEnum.BlackWhite => WICPixelFormatGUIDs.WICPixelFormatBlackWhite,\n            PixelFormatEnum.Gray2 => WICPixelFormatGUIDs.WICPixelFormat2bppGray,\n            PixelFormatEnum.Gray4 => WICPixelFormatGUIDs.WICPixelFormat4bppGray,\n            PixelFormatEnum.Gray8 => WICPixelFormatGUIDs.WICPixelFormat8bppGray,","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/PixelFormat.cs#L205-L241","documentation":"The PixelFormat(string) constructor parses the given pixel format name against a fixed list of known WPF/WIC formats (e.g. Bgr32, Gray16, Cmyk32). If the string does not case-insensitively match any known format, it throws ArgumentException with Image_BadPixelFormat naming the offending string. WPF supports only its enumerated PixelFormat set; arbitrary format names are rejected at construction time.","triggerScenarios":"new PixelFormat(\"someString\") where the string is not one of the supported names: Default, Extended, Indexed1/2/4/8, Bgr555, Bgr32, Bgra32, Pbgra32, Gray2/4/8/16, Gray32Float, Rgb48, Rgba64, Prgba64, Rgb128Float, Rgba128Float, Prgba128Float, Cmyk32, BlackWhite.","commonSituations":"Typo in XAML or serialized pixel format strings (e.g. \"BGRA32\" is fine case-insensitively, but \"RGBA32\" or \"BGR24\" are not); round-tripping pixel format names from other imaging libraries (GDI+, Skia, OpenCV) whose naming differs; machine-generated config with invalid format.","solutions":["Use the exact supported format name; compare against PixelFormats.* properties to pick a valid one","Map foreign format names to the nearest WPF equivalent before constructing (e.g. \"RGBA8888\" -> \"Pbgra32\" or \"Bgra32\")","Validate the string against PixelFormats properties before calling the constructor","If the format is genuinely unsupported, convert the bitmap to a supported format first (e.g. FormatConvertedBitmap)"],"exampleFix":"// before\nvar pf = new PixelFormat(\"RGBA32\"); // throws\n// after\nvar pf = new PixelFormat(\"Pbgra32\"); // or use PixelFormats.Pbgra32","handlingStrategy":"validation","validationCode":"bool IsValidPixelFormatName(string s) =>\n  new[]{\"Default\",\"Extended\",\"Indexed1\",\"Indexed2\",\"Indexed4\",\"Indexed8\",\"BlackWhite\",\"Gray2\",\"Gray4\",\"Gray8\",\"Gray16\",\"Gray32Float\",\"Bgr555\",\"Bgr32\",\"Bgra32\",\"Pbgra32\",\"Rgb48\",\"Rgba64\",\"Prgba64\",\"Rgb128Float\",\"Rgba128Float\",\"Prgba128Float\",\"Cmyk32\"}\n  .Any(n => n.Equals(s, StringComparison.OrdinalIgnoreCase));","typeGuard":"bool IsPixelFormatString(object v) => v is string s && IsValidPixelFormatName(s);","tryCatchPattern":"try { var pf = new PixelFormat(name); }\ncatch (ArgumentException ex) { log(ex); pf = PixelFormats.Default; }","preventionTips":["Always prefer PixelFormats.* static properties over raw strings","Map external library format names to WPF equivalents explicitly","Case-insensitively match but verify spelling against the supported list","Convert unsupported source bitmaps with FormatConvertedBitmap before use"],"tags":["wpf","imaging","pixelformat","argument"],"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-22T01:17:13.364Z"}