MonoGame/MonoGame · error · ArgumentOutOfRangeException
Texture width must be greater than zero
Error message
Texture width must be greater than zero
What it means
Thrown by the Texture2D constructor as an ArgumentOutOfRangeException when width <= 0. GPU textures must have positive dimensions; the constructor also computes TexelWidth = 1f/width, so a zero width would later cause division issues and is rejected up front.
Source
Thrown at MonoGame.Framework/Graphics/Texture2D.cs:223
/// <param name="shared">
/// Whether this render target resource should be a shared resource accessible on another device.
/// This property is only valid for DirectX targets.
/// </param>
/// <param name="arraySize">The size of the texture array.</param>
/// <exception cref="ArgumentNullException">The <paramref name="graphicsDevice"/> parameter is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// The <paramref name="width"/> and/or <paramref name="height"/> less than or equal to zero.
/// </exception>
/// <exception cref="ArgumentException">
/// The <paramref name="arraySize"/> parameter is greater than 1 and the graphics device does not support
/// texture arrays.
/// </exception>
protected Texture2D(GraphicsDevice graphicsDevice, int width, int height, bool mipmap, SurfaceFormat format, SurfaceType type, bool shared, int arraySize)
{
if (graphicsDevice == null)
throw new ArgumentNullException("graphicsDevice", FrameworkResources.ResourceCreationWhenDeviceIsNull);
if (width <= 0)
throw new ArgumentOutOfRangeException("width","Texture width must be greater than zero");
if (height <= 0)
throw new ArgumentOutOfRangeException("height","Texture height must be greater than zero");
if (arraySize > 1 && !graphicsDevice.GraphicsCapabilities.SupportsTextureArrays)
throw new ArgumentException("Texture arrays are not supported on this graphics device", "arraySize");
this.GraphicsDevice = graphicsDevice;
this.width = width;
this.height = height;
this.TexelWidth = 1f / (float)width;
this.TexelHeight = 1f / (float)height;
this._format = format;
this._levelCount = mipmap ? CalculateMipLevels(width, height) : 1;
this.ArraySize = arraySize;
// Texture will be assigned by the swap chain.
if (type == SurfaceType.SwapChainRenderTarget)
return;View on GitHub (pinned to 1d71bbd0ff)
Solutions
- Validate width > 0 before constructing, using Math.Max(1, width) or an explicit guard.
- Ensure the source of the dimension (image header, config) is loaded and parsed before use.
- Log the computed width at the call site to catch zero/negative values early.
Example fix
// before
_tex = new Texture2D(GraphicsDevice, _w, _h); // _w == 0
// after
if (_w <= 0 || _h <= 0)
throw new InvalidOperationException("Texture size not ready: " + _w + "x" + _h);
_tex = new Texture2D(GraphicsDevice, _w, _h); Defensive patterns
Strategy: validation
Validate before calling
static int RequirePositiveWidth(int w)
{
if (w <= 0) throw new ArgumentOutOfRangeException(nameof(w), "width must be > 0");
return w;
} Type guard
static bool IsValidSize(int n) => n > 0;
Prevention
- Validate width > 0 before constructing textures.
- Ensure the dimension source (image header, config) is loaded before use.
- Log computed sizes at the call site during debugging.
When it happens
Trigger: Calling `new Texture2D(device, 0, h)` or `new Texture2D(device, -1, h)`; computing width from an unloaded image/metadata that defaulted to 0; passing a clamped or uninitialized size variable.
Common situations: Procedural texture sizing from data that hasn't loaded yet (default 0); off-by-one or sign errors in resize math; reading width from a header parsed as -1 on error.
Related errors
- Texture height must be greater than zero
- level must be smaller than the number of levels in this text
- arraySlice must be smaller than the ArraySize of this textur
- Rectangle must be inside the texture bounds
- Type T is of an invalid size for the format of this texture.
AI-assisted analysis of MonoGame/MonoGame@1d71bbd0ff (2026-08-13).
Data as JSON: /api/errors/47bac434cc611008.
Report an issue: GitHub.