dotnet/wpf · error · ArgumentOutOfRangeException

SR.InvalidDiameter

Error message

SR.InvalidDiameter

What it means

Stroke.HitTest(Point, double diameter) validates the tip diameter used to simulate a tap hit. It throws ArgumentOutOfRangeException(SR.InvalidDiameter) when diameter is NaN, below DrawingAttributes.MinWidth, or above DrawingAttributes.MaxWidth. The diameter must be a real width within the allowed attribute range.

Solutions

  1. Clamp diameter between DrawingAttributes.MinWidth and DrawingAttributes.MaxWidth before calling
  2. Check double.IsNaN(diameter) and substitute a default (e.g. DrawingAttributes.MinWidth)
  3. Bind the UI control feeding the diameter with min/max limits

Example fix

// before
bool hit = stroke.HitTest(pt, diameter);
// after
double d = double.IsNaN(diameter) ? DrawingAttributes.MinWidth : Math.Clamp(diameter, DrawingAttributes.MinWidth, DrawingAttributes.MaxWidth);
bool hit = stroke.HitTest(pt, d);
Defensive patterns

Strategy: validation

Validate before calling

bool valid = !double.IsNaN(diameter) && diameter >= DrawingAttributes.MinWidth && diameter <= DrawingAttributes.MaxWidth;

Type guard

static bool IsValidDiameter(double d) => !double.IsNaN(d) && d >= DrawingAttributes.MinWidth && d <= DrawingAttributes.MaxWidth;

Try / catch

try { hit = stroke.HitTest(point, d); } catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "diameter") { hit = stroke.HitTest(point, DrawingAttributes.MinWidth); }

Prevention

When it happens

Trigger: Calling HitTest(point, double.NaN); calling with diameter 0 or negative; calling with a diameter exceeding DrawingAttributes.MaxWidth.

Common situations: Binding a slider/zoom factor to the tip diameter without clamping; NaN from a failed calculation or uninitialized double passed as the tip size.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Ink/Stroke2.cs:195

        /// </summary>
        /// <param name="point">The location to do the hitest</param>
        /// <returns>True is this stroke is hit, false otherwise</returns>
        public bool HitTest(Point point)
        {
            return HitTest(new Point[]{point}, new EllipseStylusShape(TapHitPointSize, TapHitPointSize, TapHitRotation));
        }

        /// <summary>
        /// Tap-hit. Hit tests with a point.
        /// </summary>
        /// <param name="point">The location to do the hittest</param>
        /// <param name="diameter">diameter of the tip</param>
        /// <returns>true if hit, false otherwise</returns>
        public bool HitTest(Point point, double diameter)
        {
            if (Double.IsNaN(diameter) || diameter < DrawingAttributes.MinWidth || diameter > DrawingAttributes.MaxWidth)
            {
                throw new ArgumentOutOfRangeException(nameof(diameter), SR.InvalidDiameter);
            }
            return HitTest(new Point[]{point}, new EllipseStylusShape(diameter, diameter, TapHitRotation));
        }

        /// <summary>
        /// Check whether a certain percentage of the stroke is within the Rect passed in.
        /// </summary>
        /// <param name="bounds"></param>
        /// <param name="percentageWithinBounds"></param>
        /// <returns></returns>
        public bool HitTest(Rect bounds, int percentageWithinBounds)
        {
            if ((percentageWithinBounds < 0) || (percentageWithinBounds > 100))
            {
                throw new System.ArgumentOutOfRangeException(nameof(percentageWithinBounds));
            }

            if (percentageWithinBounds == 0)

View on GitHub (pinned to 81131a70a4)