dotnet/wpf · error · IndexOutOfRangeException

IndexOutOfRangeException

Error message

IndexOutOfRangeException

What it means

This is an explicit backwards-compatibility IndexOutOfRangeException. Historically the pixel copy indexed off the end of the source array and the CLR threw; the library now detects the buffer-too-small condition up front and throws the same exception type to preserve old behavior.

Solutions

  1. Ensure sourceBuffer.Length >= offsetInBytes + stride * sourceRect.Height (in elements).
  2. Compute stride as width * bytesPerPixel rounded up to 4 bytes if required by the format.
  3. Shrink sourceRect or enlarge the array before calling WritePixels.
  4. Catch IndexOutOfRangeException at the call site to report buffer-size bugs clearly.

Example fix

// before
bitmap.WritePixels(rect, new byte[stride * (rect.Height - 1)], stride, 0);
// after
bitmap.WritePixels(rect, new byte[stride * rect.Height + offset], stride, offset);
Defensive patterns

Strategy: validation

Validate before calling

int bpp = bitmap.Format.BitsPerPixel / 8;
int offsetInBytes = offset * Marshal.SizeOf(elementType);
long required = (long)stride * sourceRect.Height + offsetInBytes;
if (sourceBuffer.Length * Marshal.SizeOf(elementType) < required) throw new IndexOutOfRangeException("source buffer too small for sourceRect");

Try / catch

try { bitmap.WritePixels(rect, buf, stride, offset); } catch (IndexOutOfRangeException) { /* grow buffer and retry */ }

Prevention

When it happens

Trigger: Calling WritePixels where the supplied source buffer (after stride, offset, and sourceRect extents are computed) is smaller than required: sourceBufferSize <= offsetInBytes, i.e., the array cannot supply sourceRect.Height rows of stride bytes.

Common situations: Off-by-one in stride computation (width * bytesPerPixel), forgetting the offset when writing partial rows, or reusing a small scratch buffer with a larger sourceRect.

Related errors


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

Appendix: source

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

            // We accept arrays of arbitrary value types - but not reference types.
            if (elementType == null || !elementType.IsValueType)
            {
                throw new ArgumentException(SR.Image_InvalidArrayForPixel);
            }
            
            checked
            {
                uint offsetInBytes = (uint)offset * (uint)elementSize;
                if (offsetInBytes >= sourceBufferSize)
                {
                    // Backwards compat:
                    //
                    // The original code would throw an exception deeper in
                    // the code when it indexed off the end of the array.  We
                    // now check earlier (compat break) but throw the same
                    // exception.
                    throw new IndexOutOfRangeException();
                }

                // Backwards-Compat:
                //
                // The "sourceRect" is actually a "destinationRect", as in it
                // refers to the location where the contents are written.
                //
                // This method presumes that the pixels are copied from the
                // the specified offset (element count) in the source buffer, and
                // that no sub-byte pixel formats are used.  We handle the offset
                // later.
                int destinationX = sourceRect.X;
                int destinationY = sourceRect.Y;
                sourceRect.X = 0;
                sourceRect.Y = 0;

                // Get the address of the data in the array by pinning it.
                unsafe

View on GitHub (pinned to 81131a70a4)