dotnet/wpf · error · ArgumentException

SR.Effect_PixelFormat

Error message

SR.Effect_PixelFormat

What it means

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'.

Solutions

  1. Use one of the built-in formats from the PixelFormats class (e.g. Bgra32, Pbgra32, Rgb24)
  2. Convert/copy source pixel data into a supported format before writing to the WriteableBitmap
  3. Do not build PixelFormat from custom GUIDs for WriteableBitmap

Example fix

// before
var fmt = new PixelFormat(PixelFormatEnum.Extended); // custom format
// after
var fmt = PixelFormats.Bgra32; // built-in supported format
Defensive patterns

Strategy: validation

Validate before calling

if (format.Format == PixelFormatEnum.Extended)
{
    format = PixelFormats.Bgra32; // replace custom format with a supported one
}

Type guard

static bool IsBuiltInFormat(PixelFormat fmt) => fmt.Format != PixelFormatEnum.Extended;

Try / catch

try { wb = new WriteableBitmap(w, h, dpiX, dpiY, format, palette); }
catch (ArgumentException ex) when (ex.ParamName == "pixelFormat")
{
    wb = new WriteableBitmap(w, h, dpiX, dpiY, PixelFormats.Bgra32, palette);
}

Prevention

When it happens

Trigger: 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.

Common situations: Interop code constructing a PixelFormat from an arbitrary pixel format GUID; copying formats from codecs that registered custom formats.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs:77

            BitmapPalette palette
            )
            : base(true) // Use base class virtuals
        {
            BeginInit();

            //
            // Sanitize inputs
            //

            if (pixelFormat.Palettized && palette == null)
            {
                throw new InvalidOperationException(SR.Image_IndexedPixelFormatRequiresPalette);
            }

            if (pixelFormat.Format == PixelFormatEnum.Extended)
            {
                // We don't support third-party pixel formats yet.
                throw new ArgumentException(SR.Effect_PixelFormat, nameof(pixelFormat));
            }

            if (pixelWidth < 0)
            {
                // Backwards Compat
                HRESULT.Check((int)WinCodecErrors.WINCODEC_ERR_VALUEOVERFLOW);
            }

            if (pixelWidth == 0)
            {
                // Backwards Compat
                HRESULT.Check(MS.Win32.NativeMethods.E_INVALIDARG);
            }

            if (pixelHeight < 0)
            {
                // Backwards Compat
                HRESULT.Check((int)WinCodecErrors.WINCODEC_ERR_VALUEOVERFLOW);

View on GitHub (pinned to 81131a70a4)