{"record":{"id":"01cd19bf1d551076","repo":"dotnet/wpf","slug":"sr-effect-pixelformat-writeablebitmap","errorCode":null,"errorMessage":"SR.Effect_PixelFormat","messagePattern":"SR\\.Effect_PixelFormat","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs","lineNumber":77,"sourceCode":"            BitmapPalette palette\n            )\n            : base(true) // Use base class virtuals\n        {\n            BeginInit();\n\n            //\n            // Sanitize inputs\n            //\n\n            if (pixelFormat.Palettized && palette == null)\n            {\n                throw new InvalidOperationException(SR.Image_IndexedPixelFormatRequiresPalette);\n            }\n\n            if (pixelFormat.Format == PixelFormatEnum.Extended)\n            {\n                // We don't support third-party pixel formats yet.\n                throw new ArgumentException(SR.Effect_PixelFormat, nameof(pixelFormat));\n            }\n\n            if (pixelWidth < 0)\n            {\n                // Backwards Compat\n                HRESULT.Check((int)WinCodecErrors.WINCODEC_ERR_VALUEOVERFLOW);\n            }\n\n            if (pixelWidth == 0)\n            {\n                // Backwards Compat\n                HRESULT.Check(MS.Win32.NativeMethods.E_INVALIDARG);\n            }\n\n            if (pixelHeight < 0)\n            {\n                // Backwards Compat\n                HRESULT.Check((int)WinCodecErrors.WINCODEC_ERR_VALUEOVERFLOW);","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs#L59-L95","documentation":"WriteableBitmap does not support third-party (extended) pixel formats. If pixelFormat.Format == PixelFormatEnum.Extended, the constructor throws ArgumentException with SR.Effect_PixelFormat and parameter name 'pixelFormat'.","triggerScenarios":"new WriteableBitmap(...) with a PixelFormat created from a non-WIC-registered custom GUID (PixelFormats will report Extended), i.e. any format not one of the built-in ones.","commonSituations":"Interop code constructing a PixelFormat from an arbitrary pixel format GUID; copying formats from codecs that registered custom formats.","solutions":["Use one of the built-in formats from the PixelFormats class (e.g. Bgra32, Pbgra32, Rgb24)","Convert/copy source pixel data into a supported format before writing to the WriteableBitmap","Do not build PixelFormat from custom GUIDs for WriteableBitmap"],"exampleFix":"// before\nvar fmt = new PixelFormat(PixelFormatEnum.Extended); // custom format\n// after\nvar fmt = PixelFormats.Bgra32; // built-in supported format","handlingStrategy":"validation","validationCode":"if (format.Format == PixelFormatEnum.Extended)\n{\n    format = PixelFormats.Bgra32; // replace custom format with a supported one\n}","typeGuard":"static bool IsBuiltInFormat(PixelFormat fmt) => fmt.Format != PixelFormatEnum.Extended;","tryCatchPattern":"try { wb = new WriteableBitmap(w, h, dpiX, dpiY, format, palette); }\ncatch (ArgumentException ex) when (ex.ParamName == \"pixelFormat\")\n{\n    wb = new WriteableBitmap(w, h, dpiX, dpiY, PixelFormats.Bgra32, palette);\n}","preventionTips":["Only pass formats obtained from the PixelFormats class","Convert custom codec formats to a built-in format before constructing WriteableBitmap"],"tags":["wpf","bitmap","argumentexception","pixelformat","unsupported"],"backgroundTag":"unsupported-dtype","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"}