dotnet/wpf · error · ArgumentOutOfRangeException

ArgumentOutOfRangeException(nameof(percentageWithinLasso))

Error message

ArgumentOutOfRangeException(nameof(percentageWithinLasso))

What it means

Stroke.HitTest(IEnumerable<Point> lassoPoints, int percentageWithinLasso) checks what percentage of the stroke falls inside a lasso polygon. percentageWithinLasso must be 0-100 or ArgumentOutOfRangeException(nameof(percentageWithinLasso)) is thrown; 0 returns true immediately. Null lassoPoints is also rejected via ArgumentNullException.

Solutions

  1. Clamp percentageWithinLasso to 0-100 before calling
  2. Validate the sensitivity setting when it is loaded from config/UI
  3. Also ensure lassoPoints is non-null and non-empty before invoking

Example fix

// before
bool hit = stroke.HitTest(lasso, pct);
// after
int pct = Math.Clamp(pct, 0, 100);
bool hit = stroke.HitTest(lasso, pct);
Defensive patterns

Strategy: validation

Validate before calling

bool valid = percentageWithinLasso is >= 0 and <= 100 && lassoPoints is not null && lassoPoints.Any();

Type guard

static bool IsValidLassoHit(IEnumerable<Point>? pts, int pct) => pts is not null && pts.Any() && pct is >= 0 and <= 100;

Try / catch

try { hit = stroke.HitTest(lasso, pct); } catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "percentageWithinLasso") { hit = stroke.HitTest(lasso, Math.Clamp(pct, 0, 100)); }

Prevention

When it happens

Trigger: Passing a negative or >100 percentage from an unvalidated UI setting; passing null or an empty lasso point collection alongside the bad percentage.

Common situations: Lasso-selection sensitivity sliders bound without range checks; computed percentages from coverage math that exceeded 100 due to rounding or bad totals.

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

Appendix: source

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

            {
                //detach from event handlers, or else we leak.
                strokeInfo?.Detach();
            }
        }

        /// <summary>
        /// Check whether a certain percentage of the stroke is within the lasso
        /// </summary>
        /// <param name="lassoPoints"></param>
        /// <param name="percentageWithinLasso"></param>
        /// <returns></returns>
        public bool HitTest(IEnumerable<Point> lassoPoints, int percentageWithinLasso)
        {
            ArgumentNullException.ThrowIfNull(lassoPoints);

            if ((percentageWithinLasso < 0) || (percentageWithinLasso > 100))
            {
                throw new System.ArgumentOutOfRangeException(nameof(percentageWithinLasso));
            }

            if (percentageWithinLasso == 0)
            {
                return true;
            }


            StrokeInfo strokeInfo = null;
            try
            {
                strokeInfo = new StrokeInfo(this);

                StylusPointCollection stylusPoints = strokeInfo.StylusPoints;
                double target = strokeInfo.TotalWeight * percentageWithinLasso / 100.0f - PercentageTolerance;

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

View on GitHub (pinned to 81131a70a4)