dotnet/wpf · error · ArgumentException

SR.Image_InsufficientBufferSize

Error message

SR.Image_InsufficientBufferSize

What it means

After computing the required buffer size (sourceRect area * bytes per pixel), WritePixelsImpl finds the caller-supplied sourceBufferSize too small. In the modern (non-backwardsCompat) path it throws ArgumentException(SR.Image_InsufficientBufferSize) naming sourceBufferSize; in the backwards-compat path it surfaces the WIC error WINCODEC_ERR_INSUFFICIENTBUFFER via HRESULT.Check instead.

Solutions

  1. Compute sourceBufferSize from the bitmap's actual format: Format.InternalBitsPerPixel / 8 * rect.Width * rect.Height.
  2. Use _format.BytesPerPixel (or source.Format) rather than a hard-coded 4.
  3. Grow or reallocate the native buffer to at least the required size before the call.
  4. In compat paths, catch the HRESULT WINCODEC_ERR_INSUFFICIENTBUFFER and surface a clear message.

Example fix

// before
int size = rect.Width * rect.Height * 4; // assumed BGRA32
// after
int size = rect.Width * rect.Height * (bitmap.Format.BitsPerPixel / 8);
Defensive patterns

Strategy: validation

Validate before calling

int bpp = bitmap.Format.BitsPerPixel / 8;
long required = (long)bpp * sourceRect.Width * sourceRect.Height;
if (sourceBufferSize < required) throw new ArgumentException($"need {required} bytes, have {sourceBufferSize}");

Try / catch

try { WritePixels(...); } catch (ArgumentException e) when (e.ParamName == "sourceBufferSize") { /* reallocate and retry */ }

Prevention

When it happens

Trigger: WritePixelsImpl called with a buffer whose size is less than sourceRect.Width*sourceRect.Height*bytesPerPixel (per row: less than width*bpp), e.g., stride/height computed for a different pixel format than the bitmap's _format.

Common situations: Computing buffer size for BGRA32 while the bitmap is 64-bit (Pbgra64), underestimating stride, or passing IntPtr buffer plus a size constant for another format.

Understand the failure class

Background: payload too large / request exceeds maximum size: why libraries cap bytes and how to fix oversize payloads — this error's family across 50 libraries.

Related errors


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

Appendix: source

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

                // Nothing to do.
                return;
            }

            checked
            {
                uint finalRowWidthInBits = (uint)((sourceRect.X + sourceRect.Width) * _format.InternalBitsPerPixel);
                uint finalRowWidthInBytes = ((finalRowWidthInBits + 7) / 8);
                uint requiredBufferSize = ((uint)(sourceRect.Y + sourceRect.Height - 1) * (uint)sourceBufferStride) + finalRowWidthInBytes;
                if (sourceBufferSize < requiredBufferSize)
                {
                    if (backwardsCompat)
                    {
                        HRESULT.Check((int)WinCodecErrors.WINCODEC_ERR_INSUFFICIENTBUFFER);
                    }
                    else
                    {
                        throw new ArgumentException(SR.Image_InsufficientBufferSize, nameof(sourceBufferSize));
                    }
                }

                uint copyWidthInBits = (uint)(sourceRect.Width * _format.InternalBitsPerPixel);

                // Calculate some offsets that we'll need in a moment.
                uint sourceXbyteOffset = (uint)((sourceRect.X * _format.InternalBitsPerPixel) / 8);
                uint sourceBufferBitOffset = (uint)((sourceRect.X * _format.InternalBitsPerPixel) % 8);
                uint firstPixelByteOffet = (uint)((sourceRect.Y * sourceBufferStride) + sourceXbyteOffset);
                uint destXbyteOffset = (uint)((destinationX * _format.InternalBitsPerPixel) / 8);
                uint destBufferBitOffset = (uint)((destinationX * _format.InternalBitsPerPixel) % 8);

                Int32Rect destinationRect = sourceRect;
                destinationRect.X = destinationX;
                destinationRect.Y = destinationY;

                //
                // Copy pixel information from the user supplied buffer to the back buffer.

View on GitHub (pinned to 81131a70a4)