dotnet/wpf · error · ArgumentOutOfRangeException

SR.InvalidDiameter

Error message

SR.InvalidDiameter

What it means

StrokeCollection.HitTest(Point, double diameter) validates the circle diameter against DrawingAttributes.MinWidth/MaxWidth before performing the point hit test. A diameter that is NaN, below MinWidth, or above MaxWidth is rejected with ArgumentOutOfRangeException. The diameter defines the size of the hit-test circle.

Solutions

  1. Clamp the diameter: Math.Max(DrawingAttributes.MinWidth, Math.Min(DrawingAttributes.MaxWidth, d))
  2. Validate before the call: !double.IsNaN(d) && d >= DrawingAttributes.MinWidth && d <= DrawingAttributes.MaxWidth
  3. Check the source of the diameter value for NaN propagation from division by zero

Example fix

// before
var hits = strokes.HitTest(point, userRadius * 2 * zoom); // may be NaN/huge
// after
var d = Math.Clamp(userRadius * 2 * zoom, DrawingAttributes.MinWidth, DrawingAttributes.MaxWidth);
if (!double.IsNaN(d)) { var hits = strokes.HitTest(point, d); }
Defensive patterns

Strategy: validation

Validate before calling

if (double.IsNaN(diameter) || diameter < DrawingAttributes.MinWidth || diameter > DrawingAttributes.MaxWidth) return new StrokeCollection();

Try / catch

try { return strokes.HitTest(point, d); } catch (ArgumentOutOfRangeException) { return new StrokeCollection(); }

Prevention

When it happens

Trigger: Calling HitTest(point, diameter) with double.NaN, a negative/zero diameter, or a diameter larger than DrawingAttributes.MaxWidth (e.g. passing a computed value from a scale transform that overflowed or a UI value in the wrong unit).

Common situations: Binding the diameter to a zoom slider that can reach 0 or NaN (NaN from 0/0 division), or passing a radius where a diameter is expected, or mixing device pixels with device-independent units.

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/11af567502ebcb7a. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Ink/StrokeCollection2.cs:66

        /// Tap-hit. Hit tests all strokes within a point, and returns a StrokeCollection for these strokes.Internally does Stroke.HitTest(Point, 1pxlRectShape).
        /// </summary>
        /// <returns>A StrokeCollection that either empty or contains the top hit stroke</returns>
        public StrokeCollection HitTest(Point point)
        {
            return PointHitTest(point, new RectangleStylusShape(1f, 1f));
        }

        /// <summary>
        /// Tap-hit
        /// </summary>
        /// <param name="point">The central point</param>
        /// <param name="diameter">The diameter value of the circle</param>
        /// <returns>A StrokeCollection that either empty or contains the top hit stroke</returns>
        public StrokeCollection HitTest(Point point, double diameter)
        {
            if (Double.IsNaN(diameter) || diameter < DrawingAttributes.MinWidth || diameter > DrawingAttributes.MaxWidth)
            {
                throw new ArgumentOutOfRangeException(nameof(diameter), SR.InvalidDiameter);
            }
            return PointHitTest(point, new EllipseStylusShape(diameter, diameter));
        }

        /// <summary>
        /// Hit-testing with lasso
        /// </summary>
        /// <param name="lassoPoints">points making the lasso</param>
        /// <param name="percentageWithinLasso">the margin value to tell whether a stroke
        /// is in or outside of the rect</param>
        /// <returns>collection of strokes found inside the rectangle</returns>
        public StrokeCollection HitTest(IEnumerable<Point> lassoPoints, int percentageWithinLasso)
        {
            // Check the input parameters
            ArgumentNullException.ThrowIfNull(lassoPoints);
            if ((percentageWithinLasso < 0) || (percentageWithinLasso > 100))
            {
                throw new System.ArgumentOutOfRangeException(nameof(percentageWithinLasso));

View on GitHub (pinned to 81131a70a4)