dotnet/wpf · error · ArgumentException
SR.Image_BadPixelFormat
Error message
SR.Image_BadPixelFormat
What it means
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.
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)
Example fix
// before
var pf = new PixelFormat("RGBA32"); // throws
// after
var pf = new PixelFormat("Pbgra32"); // or use PixelFormats.Pbgra32 Defensive patterns
Strategy: validation
Validate before calling
bool IsValidPixelFormatName(string s) =>
new[]{"Default","Extended","Indexed1","Indexed2","Indexed4","Indexed8","BlackWhite","Gray2","Gray4","Gray8","Gray16","Gray32Float","Bgr555","Bgr32","Bgra32","Pbgra32","Rgb48","Rgba64","Prgba64","Rgb128Float","Rgba128Float","Prgba128Float","Cmyk32"}
.Any(n => n.Equals(s, StringComparison.OrdinalIgnoreCase)); Type guard
bool IsPixelFormatString(object v) => v is string s && IsValidPixelFormatName(s);
Try / catch
try { var pf = new PixelFormat(name); }
catch (ArgumentException ex) { log(ex); pf = PixelFormats.Default; } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- SR.Collection_BadRank
- SR.Collection_BadRank
- SR.Format(SR.General_Expected_Type, nameof(PixelFormat))
- SR.Image_AlphaThresholdOutOfRange
- SR.Image_InvalidArrayForPixel
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/245b81afa7a9ca7e.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/PixelFormat.cs:223
_ when pixelFormatString.Equals("Gray8", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Gray8,
_ when pixelFormatString.Equals("Bgr555", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Bgr555,
_ when pixelFormatString.Equals("Bgr565", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Bgr565,
_ when pixelFormatString.Equals("Bgr24", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Bgr24,
_ when pixelFormatString.Equals("Rgb24", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Rgb24,
_ when pixelFormatString.Equals("Bgr101010", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Bgr101010,
_ when pixelFormatString.Equals("Bgr32", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Bgr32,
_ when pixelFormatString.Equals("Bgra32", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Bgra32,
_ when pixelFormatString.Equals("Pbgra32", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Pbgra32,
_ when pixelFormatString.Equals("Rgb48", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Rgb48,
_ when pixelFormatString.Equals("Rgba64", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Rgba64,
_ when pixelFormatString.Equals("Prgba64", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Prgba64,
_ when pixelFormatString.Equals("Gray16", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Gray16,
_ when pixelFormatString.Equals("Gray32Float", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Gray32Float,
_ when pixelFormatString.Equals("Rgb128Float", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Rgb128Float,
_ when pixelFormatString.Equals("Rgba128Float", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Rgba128Float,
_ when pixelFormatString.Equals("Prgba128Float", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Prgba128Float,
_ when pixelFormatString.Equals("Cmyk32", StringComparison.OrdinalIgnoreCase) => PixelFormatEnum.Cmyk32,
_ => throw new ArgumentException(SR.Format(SR.Image_BadPixelFormat, pixelFormatString), nameof(pixelFormatString)),
};
_flags = GetPixelFormatFlagsFromEnum(_format);
_bitsPerPixel = GetBitsPerPixelFromEnum(_format);
_guidFormat = GetGuidFromFormat(_format);
}
private static Guid GetGuidFromFormat(PixelFormatEnum format) => format switch
{
PixelFormatEnum.Default => WICPixelFormatGUIDs.WICPixelFormatDontCare,
PixelFormatEnum.Indexed1 => WICPixelFormatGUIDs.WICPixelFormat1bppIndexed,
PixelFormatEnum.Indexed2 => WICPixelFormatGUIDs.WICPixelFormat2bppIndexed,
PixelFormatEnum.Indexed4 => WICPixelFormatGUIDs.WICPixelFormat4bppIndexed,
PixelFormatEnum.Indexed8 => WICPixelFormatGUIDs.WICPixelFormat8bppIndexed,
PixelFormatEnum.BlackWhite => WICPixelFormatGUIDs.WICPixelFormatBlackWhite,
PixelFormatEnum.Gray2 => WICPixelFormatGUIDs.WICPixelFormat2bppGray,
PixelFormatEnum.Gray4 => WICPixelFormatGUIDs.WICPixelFormat4bppGray,
PixelFormatEnum.Gray8 => WICPixelFormatGUIDs.WICPixelFormat8bppGray,View on GitHub (pinned to 81131a70a4)