MonoGame/MonoGame · error · ArgumentException
Rectangle must be inside the texture bounds
Error message
Rectangle must be inside the texture bounds
What it means
Thrown by Texture2D.ValidateParams as an ArgumentException when the supplied Rectangle is not fully inside the texture's bounds for the given level (or has non-positive width/height). Bounds are computed as (0,0, max(width>>level,1), max(height>>level,1)); a null rect defaults to the full bounds and always passes.
Source
Thrown at MonoGame.Framework/Graphics/Texture2D.cs:788
{
uint pixel = (uint)pixels[i];
pixels[i] = (int)((pixel & 0xFF00FF00) | ((pixel & 0x00FF0000) >> 16) | ((pixel & 0x000000FF) << 16));
}
}
private void ValidateParams<T>(int level, int arraySlice, Rectangle? rect, T[] data,
int startIndex, int elementCount, out Rectangle checkedRect) where T : struct
{
var textureBounds = new Rectangle(0, 0, Math.Max(width >> level, 1), Math.Max(height >> level, 1));
checkedRect = rect ?? textureBounds;
if (level < 0 || level >= LevelCount)
throw new ArgumentException("level must be smaller than the number of levels in this texture.", "level");
if (arraySlice > 0 && !GraphicsDevice.GraphicsCapabilities.SupportsTextureArrays)
throw new ArgumentException("Texture arrays are not supported on this graphics device", "arraySlice");
if (arraySlice < 0 || arraySlice >= ArraySize)
throw new ArgumentException("arraySlice must be smaller than the ArraySize of this texture and larger than 0.", "arraySlice");
if (!textureBounds.Contains(checkedRect) || checkedRect.Width <= 0 || checkedRect.Height <= 0)
throw new ArgumentException("Rectangle must be inside the texture bounds", "rect");
if (data == null)
throw new ArgumentNullException("data");
var tSize = ReflectionHelpers.FastSizeOf<T>();
var fSize = Format.GetSize();
if (tSize > fSize || fSize % tSize != 0)
throw new ArgumentException("Type T is of an invalid size for the format of this texture.", "T");
if (startIndex < 0 || startIndex >= data.Length)
throw new ArgumentException("startIndex must be at least zero and smaller than data.Length.", "startIndex");
if (data.Length < startIndex + elementCount)
throw new ArgumentException("The data array is too small.");
int dataByteSize;
if (Format.IsCompressedFormat())
{
int blockWidth, blockHeight;
Format.GetBlockSize(out blockWidth, out blockHeight);
// round x and y down to next multiple of block size; width and height up to next multiple of block size
// we need to use this rather than the old code where because ASTC Compressed Textures are NOT Powers of 2.View on GitHub (pinned to 1d71bbd0ff)
Solutions
- Clamp the rect to the level's bounds: Rectangle.Intersect(rect, textureBounds) before the call.
- Ensure rect.Width and rect.Height are positive.
- Pass null for the rect when you want the whole level.
Example fix
// before var r = new Rectangle(-5, 0, tex.Width, tex.Height); tex.GetData(0, r, data, 0, data.Length); // throws 457 // after var bounds = new Rectangle(0, 0, tex.Width, tex.Height); var r = Rectangle.Intersect(requestedRect, bounds); if (r.Width <= 0 || r.Height <= 0) return; tex.GetData(0, r, data, 0, data.Length);
Defensive patterns
Strategy: validation
Validate before calling
static Rectangle ClampRect(Texture2D tex, int level, Rectangle rect)
{
var bounds = new Rectangle(0, 0, Math.Max(tex.Width >> level, 1), Math.Max(tex.Height >> level, 1));
var r = Rectangle.Intersect(rect, bounds);
if (r.Width <= 0 || r.Height <= 0) throw new ArgumentOutOfRangeException(nameof(rect), "rect outside texture bounds");
return r;
} Type guard
static bool IsInside(Texture2D tex, int level, Rectangle rect)
{
var bounds = new Rectangle(0, 0, Math.Max(tex.Width >> level, 1), Math.Max(tex.Height >> level, 1));
return bounds.Contains(rect) && rect.Width > 0 && rect.Height > 0;
} Prevention
- Intersect the rect with the level's bounds before GetData/SetData.
- Ensure rect.Width/Height are positive.
- Pass null for the rect when you want the entire level.
When it happens
Trigger: Calling GetData/SetData with a rect whose X/Y is negative, whose right/bottom edge exceeds the (mip-level-scaled) texture size, or whose Width/Height <= 0; computing a sub-region from mouse/scroll coordinates that fall outside the texture.
Common situations: Pixel picking/scissor math that produces out-of-range rects; cropping a sub-texture with an off-by-one; forgetting that higher mip levels shrink the valid bounds.
Related errors
- level must be smaller than the number of levels in this text
- arraySlice must be smaller than the ArraySize of this textur
- Type T is of an invalid size for the format of this texture.
- Texture width must be greater than zero
- Texture height must be greater than zero
AI-assisted analysis of MonoGame/MonoGame@1d71bbd0ff (2026-08-13).
Data as JSON: /api/errors/6e3a5b53976f3bdc.
Report an issue: GitHub.