dotnet/wpf · error · ArgumentOutOfRangeException

ArgumentOutOfRangeException(nameof(percentageWithinBounds))

Error message

ArgumentOutOfRangeException(nameof(percentageWithinBounds))

What it means

Stroke.HitTest(Rect bounds, int percentageWithinBounds) checks whether a percentage of the stroke lies inside a rectangle. The percentage is validated to be within 0-100; outside that range ArgumentOutOfRangeException(nameof(percentageWithinBounds)) is thrown. A value of 0 short-circuits to true.

Solutions

  1. Clamp the value: Math.Clamp(percentageWithinBounds, 0, 100) before calling
  2. Validate user/config input at load time and reject or normalize values outside 0-100
  3. Use Math.Round on computed percentages and verify the range

Example fix

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

Strategy: validation

Validate before calling

bool valid = percentageWithinBounds is >= 0 and <= 100;

Type guard

static bool IsValidPercentage(int p) => p is >= 0 and <= 100;

Try / catch

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

Prevention

When it happens

Trigger: Passing a negative percentage or a value greater than 100, often from an unclamped slider value, parsed user input, or a bad config constant.

Common situations: Selection sensitivity settings in ink editors configured via config files with out-of-range values; arithmetic producing percentages outside 0-100 (e.g. dividing incorrectly).

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

Appendix: source

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

        {
            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)
            {
                return true;
            }

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

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

                for (int i = 0; i < stylusPoints.Count; i++)
                {
                    if (bounds.Contains((Point)stylusPoints[i]))

View on GitHub (pinned to 81131a70a4)