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
- Clamp or verify y against buffer.Height before the call
- Fix loop bounds to 'for (int y = 0; y < buffer.Height; y++)'
- 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
- Use strict '<' comparisons with the same buffer's Height in loops
- Derive y from the buffer itself, not a different image
- Prefer foreach over buffer spans to avoid manual indexing
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
- Attempted to allocate a buffer of negative length=
- Column regions should not overlap!
- Trying to copy/swap incompatible buffers. This is most…
- Wrap: buffers should be uniformly sized!
- Wrap: the last buffer is too large!
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)