dotnet/wpf · error · ArgumentException
SR.Effect_PixelFormat
Error message
SR.Effect_PixelFormat
What it means
PixelFormats.Default ('don't care' — let the system pick) is not a valid source format for CachedBitmap: when created from raw memory the format must be known exactly to interpret the buffer. Passing Default (GUID WICPixelFormatDontCare) throws an ArgumentException naming the offending format.
Solutions
- Pass an explicit concrete format matching the buffer, e.g. PixelFormats.Bgra32, Bgr32, Gray8, Pbgra32
- Ensure the format matches the element size and stride of the pixel array
- Replace default-initialized PixelFormat fields with a real format before constructing
Example fix
// before var bmp = new CachedBitmap(pixels, w, h, 96, 96, PixelFormats.Default, null, stride); // after var bmp = new CachedBitmap(pixels, w, h, 96, 96, PixelFormats.Bgra32, null, stride);
Defensive patterns
Strategy: validation
Validate before calling
if (format == PixelFormats.Default)
format = PixelFormats.Bgra32; // pick a concrete format matching your buffer Type guard
static bool IsConcrete(PixelFormat f) => f != PixelFormats.Default && f.Guid != Guid.Empty;
Try / catch
try { return new CachedBitmap(pixels, w, h, dpiX, dpiY, fmt, palette, stride); }
catch (ArgumentException ex) when (ex.ParamName == "pixelFormat") { return new CachedBitmap(pixels, w, h, dpiX, dpiY, PixelFormats.Bgra32, palette, stride); } Prevention
- Never use PixelFormats.Default with from-memory bitmap construction
- Derive the format from the buffer's element size (1B=Gray8, 4B=Bgra32, etc.)
- Initialize PixelFormat fields to a concrete default, not Default
When it happens
Trigger: new CachedBitmap(pixels, w, h, dpiX, dpiY, PixelFormats.Default, palette, stride) — explicitly passing the Default PixelFormat for a from-memory construction.
Common situations: Copying code from BitmapSource usage where Default is sometimes acceptable; a PixelFormat variable that was never initialized and defaulted to Default; generic image pipelines that defer format choice.
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
- Image_EncoderNoColorContext
- Image_EncoderNoGlobalMetadata
- Image_EncoderNoGlobalThumbnail
- Image_EncoderNoPreview
- Image_GuidEmpty (containerFormat)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/f5b9a12ae26f761a.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/CachedBitmap.cs:348
///
private void InitFromMemoryPtr(
int pixelWidth,
int pixelHeight,
double dpiX,
double dpiY,
PixelFormat pixelFormat,
BitmapPalette palette,
IntPtr buffer,
int bufferSize,
int stride
)
{
if (pixelFormat.Palettized && palette == null)
throw new InvalidOperationException(SR.Image_IndexedPixelFormatRequiresPalette);
if (pixelFormat.Format == PixelFormatEnum.Default && pixelFormat.Guid == WICPixelFormatGUIDs.WICPixelFormatDontCare)
{
throw new System.ArgumentException(
SR.Format(SR.Effect_PixelFormat, pixelFormat),
nameof(pixelFormat)
);
}
_bitmapInit.BeginInit();
try
{
BitmapSourceSafeMILHandle wicBitmap;
// Create the unmanaged resources
Guid guidFmt = pixelFormat.Guid;
using (FactoryMaker factoryMaker = new FactoryMaker())
{
HRESULT.Check(UnsafeNativeMethods.WICImagingFactory.CreateBitmapFromMemory(
factoryMaker.ImagingFactoryPtr,View on GitHub (pinned to 81131a70a4)