{"record":{"id":"47bac434cc611008","repo":"MonoGame/MonoGame","slug":"texture-width-must-be-greater-than-zero","errorCode":null,"errorMessage":"Texture width must be greater than zero","messagePattern":"Texture width must be greater than zero","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MonoGame.Framework/Graphics/Texture2D.cs","lineNumber":223,"sourceCode":"        /// <param name=\"shared\">\n        /// Whether this render target resource should be a shared resource accessible on another device.\n        /// This property is only valid for DirectX targets.\n        /// </param>\n        /// <param name=\"arraySize\">The size of the texture array.</param>\n        /// <exception cref=\"ArgumentNullException\">The <paramref name=\"graphicsDevice\"/> parameter is null.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\">\n        /// The <paramref name=\"width\"/> and/or <paramref name=\"height\"/> less than or equal to zero.\n        /// </exception>\n        /// <exception cref=\"ArgumentException\">\n        /// The <paramref name=\"arraySize\"/> parameter is greater than 1 and the graphics device does not support\n        /// texture arrays.\n        /// </exception>\n        protected Texture2D(GraphicsDevice graphicsDevice, int width, int height, bool mipmap, SurfaceFormat format, SurfaceType type, bool shared, int arraySize)\n\t\t{\n            if (graphicsDevice == null)\n                throw new ArgumentNullException(\"graphicsDevice\", FrameworkResources.ResourceCreationWhenDeviceIsNull);\n            if (width <= 0)\n                throw new ArgumentOutOfRangeException(\"width\",\"Texture width must be greater than zero\");\n            if (height <= 0)\n                throw new ArgumentOutOfRangeException(\"height\",\"Texture height must be greater than zero\");\n            if (arraySize > 1 && !graphicsDevice.GraphicsCapabilities.SupportsTextureArrays)\n                throw new ArgumentException(\"Texture arrays are not supported on this graphics device\", \"arraySize\");\n\n            this.GraphicsDevice = graphicsDevice;\n            this.width = width;\n            this.height = height;\n            this.TexelWidth = 1f / (float)width;\n            this.TexelHeight = 1f / (float)height;\n\n            this._format = format;\n            this._levelCount = mipmap ? CalculateMipLevels(width, height) : 1;\n            this.ArraySize = arraySize;\n\n            // Texture will be assigned by the swap chain.\n\t\t    if (type == SurfaceType.SwapChainRenderTarget)\n\t\t        return;","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/MonoGame/MonoGame/blob/1d71bbd0ff4071a03caa583aa3bc56b85924a819/MonoGame.Framework/Graphics/Texture2D.cs#L205-L241","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n_tex = new Texture2D(GraphicsDevice, _w, _h); // _w == 0\n\n// after\nif (_w <= 0 || _h <= 0)\n    throw new InvalidOperationException(\"Texture size not ready: \" + _w + \"x\" + _h);\n_tex = new Texture2D(GraphicsDevice, _w, _h);","handlingStrategy":"validation","validationCode":"static int RequirePositiveWidth(int w)\n{\n    if (w <= 0) throw new ArgumentOutOfRangeException(nameof(w), \"width must be > 0\");\n    return w;\n}","typeGuard":"static bool IsValidSize(int n) => n > 0;","tryCatchPattern":null,"preventionTips":["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."],"tags":["monogame","graphics","texture2d","validation","argument-out-of-range","dimension"],"backgroundTag":null,"analyzedSha":"1d71bbd0ff4071a03caa583aa3bc56b85924a819","analyzedAt":"2026-08-13T17:10:33.625Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}