dotnet/wpf · error · ArgumentOutOfRangeException

ArgumentOutOfRangeException(percentageWithinLasso)

Error message

ArgumentOutOfRangeException(percentageWithinLasso)

What it means

StrokeCollection.HitTest(IEnumerable<Point>, int percentageWithinLasso) requires the lasso percentage to be between 0 and 100 inclusive. Values outside that range are rejected with ArgumentOutOfRangeException naming percentageWithinLasso. The percentage controls how much of a stroke must lie inside the lasso for it to count as hit.

Solutions

  1. Convert any 0-1 fraction to a percentage: (int)Math.Round(fraction * 100)
  2. Clamp before calling: Math.Clamp(percentageWithinLasso, 0, 100)
  3. Fix the UI control bounds so the percentage cannot leave 0-100

Example fix

// before
var hits = strokes.HitTest(lassoPoints, sensitivityFraction); // 0..1
// after
int pct = (int)Math.Round(sensitivityFraction * 100);
var hits = strokes.HitTest(lassoPoints, pct); // 0..100
Defensive patterns

Strategy: validation

Validate before calling

int pct = Math.Clamp((int)Math.Round(fraction * 100), 0, 100);

Try / catch

try { return strokes.HitTest(lasso, pct); } catch (ArgumentOutOfRangeException) { return null; }

Prevention

When it happens

Trigger: Calling the lasso HitTest overload with a negative value or a value above 100, e.g. a fraction (0.0-1.0) passed instead of a percentage, or a user-configured sensitivity value that was not clamped.

Common situations: Storing hit-test sensitivity as 0-1 in settings and passing it directly; slider controls with Maximum > 100; localization of user input producing values like 150%.

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/0b1a05bbf8a1f0df. Report an issue: GitHub.

Appendix: source

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

                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));
            }

            if (IEnumerablePointHelper.GetCount(lassoPoints) < 3)
            {
                return new StrokeCollection();
            }

            Lasso lasso = new SingleLoopLasso();
            lasso.AddPoints(lassoPoints);

            // Enumerate through the strokes and collect those captured by the lasso.
            StrokeCollection lassoedStrokes = new StrokeCollection();
            foreach (Stroke stroke in this)
            {
                if (percentageWithinLasso == 0)
                {
                    lassoedStrokes.Add(stroke);
                }

View on GitHub (pinned to 81131a70a4)