{"record":{"id":"df2999603f564064","repo":"dotnet/wpf","slug":"sr-effect-pixelformat-formatted-with-pixelformat","errorCode":null,"errorMessage":"SR.Effect_PixelFormat (formatted with pixelFormat)","messagePattern":"SR\\.Effect_PixelFormat \\(formatted with pixelFormat\\)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/RenderTargetBitmap.cs","lineNumber":38,"sourceCode":"        /// <param name=\"pixelHeight\">The height of the Bitmap</param>\n        /// <param name=\"dpiX\">Horizontal DPI of the Bitmap</param>\n        /// <param name=\"dpiY\">Vertical DPI of the Bitmap</param>\n        /// <param name=\"pixelFormat\">Format of the Bitmap.</param>\n        public RenderTargetBitmap(\n            int pixelWidth,\n            int pixelHeight,\n            double dpiX,\n            double dpiY,\n            PixelFormat pixelFormat\n            ) : base(true)\n        {\n            if (pixelFormat.Format == PixelFormatEnum.Default)\n            {\n                pixelFormat = PixelFormats.Pbgra32;\n            }\n            else if (pixelFormat.Format != PixelFormatEnum.Pbgra32)\n            {\n                throw new System.ArgumentException(\n                        SR.Format(SR.Effect_PixelFormat, pixelFormat),\n                        nameof(pixelFormat)\n                        );\n            }\n\n            ArgumentOutOfRangeException.ThrowIfNegativeOrZero(pixelWidth);\n            ArgumentOutOfRangeException.ThrowIfNegativeOrZero(pixelHeight);\n\n            if (dpiX < DoubleUtil.DBL_EPSILON)\n            {\n                dpiX = 96.0;\n            }\n\n            if (dpiY < DoubleUtil.DBL_EPSILON)\n            {\n                dpiY = 96.0;\n            }\n","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/RenderTargetBitmap.cs#L20-L56","documentation":"RenderTargetBitmap's constructor only accepts PixelFormats.Pbgra32 (PixelFormats.Default is silently converted to Pbgra32). Any other pixel format throws ArgumentException formatted with SR.Effect_PixelFormat, naming the offending format.","triggerScenarios":"new RenderTargetBitmap(pixelWidth, pixelHeight, dpiX, dpiY, pixelFormat) where pixelFormat is e.g. Bgra32, Rgb24, Bgr32, or any non-Pbgra32 format.","commonSituations":"Porting rendering code from WriteableBitmap or BitmapSource.Create (which accept any format); parameterizing pixel format from config for multiple bitmap APIs; refactors that made the format a shared constant.","solutions":["Pass PixelFormats.Pbgra32 (or PixelFormats.Default) to the constructor","If another format is needed, render in Pbgra32 and convert afterwards with a FormatConvertedBitmap or pixel copy","Remove per-call format parameters and centralize render-target creation with the fixed Pbgra32 format"],"exampleFix":"// before\nvar rtb = new RenderTargetBitmap(w, h, 96, 96, PixelFormats.Bgra32);\n// after\nvar rtb = new RenderTargetBitmap(w, h, 96, 96, PixelFormats.Pbgra32);","handlingStrategy":"validation","validationCode":"if (pixelFormat != PixelFormats.Pbgra32 && pixelFormat != PixelFormats.Default)\n    throw new ArgumentException(\"RenderTargetBitmap requires Pbgra32 or Default\", nameof(pixelFormat));","typeGuard":"static bool IsValidRtbFormat(PixelFormat pf) =>\n    pf.Format is PixelFormatEnum.Pbgra32 or PixelFormatEnum.Default;","tryCatchPattern":"try { var rtb = new RenderTargetBitmap(w, h, dx, dy, pixelFormat); }\ncatch (ArgumentException ex) when (ex.ParamName == \"pixelFormat\")\n{ /* retry with PixelFormats.Pbgra32 */ }","preventionTips":["Always use PixelFormats.Pbgra32 for render targets","Never copy pixel-format arguments from BitmapSource.Create call sites to RenderTargetBitmap","Centralize render-target creation in one helper that enforces the format"],"tags":["wpf","pixelformat","argumentexception","rendertargetbitmap"],"backgroundTag":"unsupported-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"}