dotnet/wpf · error · ArgumentException

SR.Effect_PixelFormat (formatted with pixelFormat)

Error message

SR.Effect_PixelFormat (formatted with pixelFormat)

What it means

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.

Solutions

  1. Pass PixelFormats.Pbgra32 (or PixelFormats.Default) to the constructor
  2. If another format is needed, render in Pbgra32 and convert afterwards with a FormatConvertedBitmap or pixel copy
  3. Remove per-call format parameters and centralize render-target creation with the fixed Pbgra32 format

Example fix

// before
var rtb = new RenderTargetBitmap(w, h, 96, 96, PixelFormats.Bgra32);
// after
var rtb = new RenderTargetBitmap(w, h, 96, 96, PixelFormats.Pbgra32);
Defensive patterns

Strategy: validation

Validate before calling

if (pixelFormat != PixelFormats.Pbgra32 && pixelFormat != PixelFormats.Default)
    throw new ArgumentException("RenderTargetBitmap requires Pbgra32 or Default", nameof(pixelFormat));

Type guard

static bool IsValidRtbFormat(PixelFormat pf) =>
    pf.Format is PixelFormatEnum.Pbgra32 or PixelFormatEnum.Default;

Try / catch

try { var rtb = new RenderTargetBitmap(w, h, dx, dy, pixelFormat); }
catch (ArgumentException ex) when (ex.ParamName == "pixelFormat")
{ /* retry with PixelFormats.Pbgra32 */ }

Prevention

When it happens

Trigger: new RenderTargetBitmap(pixelWidth, pixelHeight, dpiX, dpiY, pixelFormat) where pixelFormat is e.g. Bgra32, Rgb24, Bgr32, or any non-Pbgra32 format.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/df2999603f564064. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/RenderTargetBitmap.cs:38

        /// <param name="pixelHeight">The height of the Bitmap</param>
        /// <param name="dpiX">Horizontal DPI of the Bitmap</param>
        /// <param name="dpiY">Vertical DPI of the Bitmap</param>
        /// <param name="pixelFormat">Format of the Bitmap.</param>
        public RenderTargetBitmap(
            int pixelWidth,
            int pixelHeight,
            double dpiX,
            double dpiY,
            PixelFormat pixelFormat
            ) : base(true)
        {
            if (pixelFormat.Format == PixelFormatEnum.Default)
            {
                pixelFormat = PixelFormats.Pbgra32;
            }
            else if (pixelFormat.Format != PixelFormatEnum.Pbgra32)
            {
                throw new System.ArgumentException(
                        SR.Format(SR.Effect_PixelFormat, pixelFormat),
                        nameof(pixelFormat)
                        );
            }

            ArgumentOutOfRangeException.ThrowIfNegativeOrZero(pixelWidth);
            ArgumentOutOfRangeException.ThrowIfNegativeOrZero(pixelHeight);

            if (dpiX < DoubleUtil.DBL_EPSILON)
            {
                dpiX = 96.0;
            }

            if (dpiY < DoubleUtil.DBL_EPSILON)
            {
                dpiY = 96.0;
            }

View on GitHub (pinned to 81131a70a4)