{"record":{"id":"f5b9a12ae26f761a","repo":"dotnet/wpf","slug":"sr-effect-pixelformat","errorCode":null,"errorMessage":"SR.Effect_PixelFormat","messagePattern":"SR\\.Effect_PixelFormat","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/CachedBitmap.cs","lineNumber":348,"sourceCode":"        ///\n        private void InitFromMemoryPtr(\n                    int pixelWidth,\n                    int pixelHeight,\n                    double dpiX,\n                    double dpiY,\n                    PixelFormat pixelFormat,\n                    BitmapPalette palette,\n                    IntPtr buffer,\n                    int bufferSize,\n                    int stride\n                    )\n        {\n            if (pixelFormat.Palettized && palette == null)\n                throw new InvalidOperationException(SR.Image_IndexedPixelFormatRequiresPalette);\n\n            if (pixelFormat.Format == PixelFormatEnum.Default && pixelFormat.Guid == WICPixelFormatGUIDs.WICPixelFormatDontCare)\n            {\n                throw new System.ArgumentException(\n                        SR.Format(SR.Effect_PixelFormat, pixelFormat),\n                        nameof(pixelFormat)\n                        );\n            }\n\n            _bitmapInit.BeginInit();\n\n            try\n            {\n                BitmapSourceSafeMILHandle wicBitmap;\n\n                // Create the unmanaged resources\n                Guid guidFmt = pixelFormat.Guid;\n\n                using (FactoryMaker factoryMaker = new FactoryMaker())\n                {\n                    HRESULT.Check(UnsafeNativeMethods.WICImagingFactory.CreateBitmapFromMemory(\n                            factoryMaker.ImagingFactoryPtr,","sourceCodeStart":330,"sourceCodeEnd":366,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/CachedBitmap.cs#L330-L366","documentation":"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.","triggerScenarios":"new CachedBitmap(pixels, w, h, dpiX, dpiY, PixelFormats.Default, palette, stride) — explicitly passing the Default PixelFormat for a from-memory construction.","commonSituations":"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.","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"],"exampleFix":"// before\nvar bmp = new CachedBitmap(pixels, w, h, 96, 96, PixelFormats.Default, null, stride);\n// after\nvar bmp = new CachedBitmap(pixels, w, h, 96, 96, PixelFormats.Bgra32, null, stride);","handlingStrategy":"validation","validationCode":"if (format == PixelFormats.Default)\n    format = PixelFormats.Bgra32; // pick a concrete format matching your buffer","typeGuard":"static bool IsConcrete(PixelFormat f) => f != PixelFormats.Default && f.Guid != Guid.Empty;","tryCatchPattern":"try { return new CachedBitmap(pixels, w, h, dpiX, dpiY, fmt, palette, stride); }\ncatch (ArgumentException ex) when (ex.ParamName == \"pixelFormat\") { return new CachedBitmap(pixels, w, h, dpiX, dpiY, PixelFormats.Bgra32, palette, stride); }","preventionTips":["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"],"tags":["wpf","imaging","pixel-format"],"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"}