dotnet/wpf · error · ArgumentNullException

SR.Image_InsufficientBuffer

Error message

SR.Image_InsufficientBuffer

What it means

ValidateArrayAndGetInfo checks the pixels array before copying. For a rank-1 array, a zero-length first dimension cannot supply any pixel data, so it throws ArgumentException(SR.Image_InsufficientBuffer) (or, per the shown region, ArgumentNullException first for a null buffer named 'pixels'/'sourceBuffer'). This guards the pin/copy path from zero-byte buffers.

Solutions

  1. Pass a non-null array with first dimension >= sourceRect.Height (or required element count).
  2. Validate array.Length > 0 and enough elements for rect*stride before calling.
  3. Guard the call: if (pixels == null || pixels.Length == 0) skip or throw a clearer error.

Example fix

// before
var pixels = new byte[0];
bitmap.WritePixels(rect, pixels, stride, 0); // throws
// after
var pixels = new byte[stride * rect.Height];
bitmap.WritePixels(rect, pixels, stride, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (sourceBuffer == null) throw new ArgumentNullException(nameof(sourceBuffer));
if (sourceBuffer.Length == 0) throw new ArgumentException("sourceBuffer must not be empty");
if (sourceBuffer.Length < sourceRect.Height * stride / elementSize) throw new ArgumentException("sourceBuffer too small");

Type guard

bool IsValidPixels<T>(T[] buf) where T : struct => buf != null && buf.Length > 0;

Try / catch

try { bitmap.WritePixels(rect, buf, stride, 0); } catch (ArgumentException e) when (e.ParamName == "sourceBuffer") { /* fix buffer */ }

Prevention

When it happens

Trigger: Calling WritePixels with a null array, or new T[0] / any rank-1 array whose first dimension is 0.

Common situations: Array allocated with the wrong dimension order (e.g., new byte[0][rows]); buffers sized from an empty sourceRect; failed deserialization yielding empty arrays.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

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

        /// </param>
        /// <param name="elementSize">
        ///     On output, will contain the size of the elements in the array.
        /// </param>
        /// <param name="sourceBufferSize">
        ///     On output, will contain the size of the array.
        /// </param>
        private static void ValidateArrayAndGetInfo(Array sourceBuffer,
                                                    bool backwardsCompat,
                                                    out int elementSize,
                                                    out uint sourceBufferSize,
                                                    out Type elementType)
        {
            //
            // Assure that a valid pixels Array was provided.
            //
            if (sourceBuffer == null)
            {
                throw new ArgumentNullException(backwardsCompat ? "pixels" : "sourceBuffer");
            }

            if (sourceBuffer.Rank == 1)
            {
                int firstDimLength = sourceBuffer.GetLength(0);
                if (firstDimLength == 0)
                {
                    if (backwardsCompat)
                    {
                        elementSize = 1;
                        sourceBufferSize = 0;
                        elementType = null;
                    }
                    else
                    {
                        throw new ArgumentException(SR.Image_InsufficientBuffer, nameof(sourceBuffer));
                    }
                }

View on GitHub (pinned to 81131a70a4)