dotnet/wpf · error · ArgumentException

SR.Format(SR.Rect_Empty, "rect")

Error message

SR.Format(SR.Rect_Empty, "rect")

What it means

The EllipseGeometry(Rect) constructor requires a non-empty rectangle because it derives radius and center from rect dimensions. Passing Rect.Empty yields meaningless geometry, so the constructor throws ArgumentException with Rect_Empty.

Solutions

  1. Check rect.IsEmpty before constructing and skip or substitute a default geometry.
  2. Ensure the Rect is computed from actual bounds (e.g. after Measure/Arrange) instead of relying on default(Rect).
  3. Use a different constructor (center/radii) when the rectangle may be degenerate.

Example fix

// before
var geo = new EllipseGeometry(rect);
// after
EllipseGeometry geo = rect.IsEmpty ? null : new EllipseGeometry(rect);
Defensive patterns

Strategy: validation

Validate before calling

if (rect.IsEmpty) throw new ArgumentException("rect must not be empty", nameof(rect));
var geo = new EllipseGeometry(rect);

Type guard

static bool IsValidRect(Rect r) => !r.IsEmpty && r.Width > 0 && r.Height > 0;

Try / catch

try { var geo = new EllipseGeometry(rect); }
catch (ArgumentException) { /* fall back to default geometry */ }

Prevention

When it happens

Trigger: Calling new EllipseGeometry(rect) where rect.IsEmpty is true — typically a Rect field/property that was never initialized (default(Rect) or Rect.Empty) rather than computed from real bounds.

Common situations: Hit-testing or geometry code that passes a layout slot before it has been measured, data-bound Rect properties defaulting to Rect.Empty, or interop code returning an empty bounding box.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/EllipseGeometry.cs:30

    public sealed partial class EllipseGeometry : Geometry 
    {
        #region Constructors

        /// <summary>
        ///
        /// </summary>
        public EllipseGeometry()
        {
        }

        /// <summary>
        /// Constructor - sets the ellipse to the paramters with the given transformation
        /// </summary>
        public EllipseGeometry(Rect rect)
        {
            if (rect.IsEmpty) 
            {
                throw new System.ArgumentException(SR.Format(SR.Rect_Empty, "rect"));
            }

            RadiusX = (rect.Right - rect.X) * (1.0 / 2.0);
            RadiusY = (rect.Bottom - rect.Y) * (1.0 / 2.0);
            Center = new Point(rect.X + RadiusX, rect.Y + RadiusY);
        }
        
        /// <summary>
        /// Constructor - sets the ellipse to the parameters
        /// </summary>
        public EllipseGeometry(
            Point center, 
            double radiusX, 
            double radiusY)
        {
            Center = center;
            RadiusX = radiusX;
            RadiusY = radiusY;

View on GitHub (pinned to 81131a70a4)