SixLabors/ImageSharp · error · ArgumentOutOfRangeException

DangerousGetRowSpan( ). Y was out of range. Height=

Error message

DangerousGetRowSpan({y}). Y was out of range. Height={this.Height}

What it means

Buffer2D<T>.DangerousGetRowSpan validates the requested row index against the buffer's Height and throws ArgumentOutOfRangeException naming the offending y and the height when it is outside [0, Height).

Solutions

  1. Clamp or verify y against buffer.Height before the call
  2. Fix loop bounds to 'for (int y = 0; y < buffer.Height; y++)'
  3. Use the buffer's own span enumeration instead of manual indexing

Example fix

// before
for (int y = 0; y <= buffer.Height; y++) Process(buffer.DangerousGetRowSpan(y));
// after
for (int y = 0; y < buffer.Height; y++) Process(buffer.DangerousGetRowSpan(y));
Defensive patterns

Strategy: validation

Validate before calling

if (y < 0 || y >= buffer.Height) throw new ArgumentOutOfRangeException(nameof(y));

Type guard

bool validRow<T>(Buffer2D<T> b, int y) => (uint)y < (uint)b.Height;

Try / catch

try { var span = buffer.DangerousGetRowSpan(y); } catch (ArgumentOutOfRangeException) { /* clamp y and retry */ }

Prevention

When it happens

Trigger: Calling DangerousGetRowSpan with y < 0 or y >= buffer.Height, often from loop bounds computed from a different buffer or an off-by-one (y <= Height).

Common situations: Manual pixel loops over ProcessedPixelRow callbacks or custom processors where the row index comes from another image's dimensions.

Related errors


AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13). Data as JSON: /api/errors/8f7b55c761d598f3. Report an issue: GitHub.

Appendix: source

Thrown at src/ImageSharp/Memory/Buffer2D{T}.cs:422

            bool destinationCanRepresentSource = destination.FastMemoryGroup.TotalLength >= sourceLayoutLength;
            bool sourceCanRepresentDestination = source.FastMemoryGroup.TotalLength >= destinationLayoutLength;
            if (!destinationCanRepresentSource || !sourceCanRepresentDestination)
            {
                throw new InvalidMemoryOperationException(
                    "Trying to copy/swap incompatible buffers. This is most likely caused by applying an unsupported processor to wrapped-memory images.");
            }

            source.CopyTo(destination);
        }

        (destination.Size, source.Size) = (source.Size, destination.Size);
        (destination.RowStride, source.RowStride) = (source.RowStride, destination.RowStride);
        return swapped;
    }

    [MethodImpl(InliningOptions.ColdPath)]
    private void ThrowYOutOfRangeException(int y)
        => throw new ArgumentOutOfRangeException($"DangerousGetRowSpan({y}). Y was out of range. Height={this.Height}");

    [MethodImpl(InliningOptions.ShortMethod)]
    private static long GetRequiredLength(int width, int height, int stride)
        => checked(((long)(height - 1) * stride) + width);
}

View on GitHub (pinned to 59ce6af6fc)