dotnet/wpf · error · ArgumentException

SR.Size_WidthAndHeightCannotBeNegative

Error message

SR.Size_WidthAndHeightCannotBeNegative

What it means

The Rect(double x, double y, double width, double height) constructor validates that neither dimension is negative and throws ArgumentException otherwise. WPF's Rect models an axis-aligned rectangle whose width and height must be zero or positive; the special 'empty' rect is represented by NaN fields, not by negative sizes. This guard exists so invariants in layout and geometry code never see malformed rectangles.

Solutions

  1. Compute width/height as Math.Abs(right - left) / Math.Abs(bottom - top), or use the Rect(Point, Point) constructor which normalizes points automatically.
  2. Validate inputs before constructing: if (width < 0 || height < 0) throw or clamp to 0.
  3. If an empty rect is intended, use Rect.Empty instead of a negative-size rect.

Example fix

// before
var rect = new Rect(left, top, right - left, bottom - top);
// after
var rect = new Rect(Math.Min(left, right), Math.Min(top, bottom),
                    Math.Abs(right - left), Math.Abs(bottom - top));
Defensive patterns

Strategy: validation

Validate before calling

public static bool IsValidExtent(double width, double height) => width >= 0 && height >= 0;
// then: if (!IsValidExtent(w, h)) w = Math.Abs(w); h = Math.Abs(h);
// or simply: var rect = new Rect(new Point(x, y), new Point(x2, y2)); // normalizes automatically

Type guard

static bool CanBuildRect(double w, double h) => !double.IsNaN(w) && !double.IsNaN(h) && w >= 0 && h >= 0;

Try / catch

try { var rect = new Rect(x, y, w, h); }
catch (ArgumentException ex) when (ex.Message.Contains("negative")) { /* normalize points and retry */ }

Prevention

When it happens

Trigger: Calling new Rect(x, y, width, height) where width < 0 or height < 0, e.g. computing width as right-left without clamping negative results.

Common situations: Subtracting coordinates in the wrong order (right - left where right < left), deserializing sizes from untrusted data, porting GDI/WinForms code where negative width meant 'flip', or computing span from swapped points without Point order normalization.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/25eaf6c046102b59. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Rect.cs:48

                _x = location._x;
                _y = location._y;
                _width = size._width;
                _height = size._height;
            }
        }

        /// <summary>
        /// Constructor which sets the initial values to the values of the parameters.
        /// Width and Height must be non-negative
        /// </summary>
        public Rect(double x,
                    double y,
                    double width,
                    double height)
        {
            if (width < 0 || height < 0)
            {
                throw new System.ArgumentException(SR.Size_WidthAndHeightCannotBeNegative);
            }

            _x    = x;
            _y     = y;
            _width   = width;
            _height  = height;
        }

        /// <summary>
        /// Constructor which sets the initial values to bound the two points provided.
        /// </summary>
        public Rect(Point point1,
                    Point point2)
        {
            _x = Math.Min(point1._x, point2._x);
            _y = Math.Min(point1._y, point2._y);

            //  Max with 0 to prevent double weirdness from causing us to be (-epsilon..0)

View on GitHub (pinned to 81131a70a4)