dotnet/wpf · error · ArgumentOutOfRangeException
ArgumentOutOfRangeException(percentageWithinBounds)
Error message
ArgumentOutOfRangeException(percentageWithinBounds)
What it means
StrokeCollection.HitTest(Rect, int percentageWithinBounds) requires percentageWithinBounds to be between 0 and 100 inclusive; other values throw ArgumentOutOfRangeException naming percentageWithinBounds. It controls what fraction of a stroke must lie inside the rectangle for a hit.
Solutions
- Clamp: Math.Clamp(percentageWithinBounds, 0, 100) before the call
- Convert fractions to percentages before passing
- Validate the value in the settings/UI layer that produces it
Example fix
// before var hits = strokes.HitTest(rect, conf); // conf in 0..1 // after var hits = strokes.HitTest(rect, Math.Clamp((int)(conf * 100), 0, 100));
Defensive patterns
Strategy: validation
Validate before calling
int pct = Math.Clamp(percentageWithinBounds, 0, 100);
Try / catch
try { return strokes.HitTest(bounds, pct); } catch (ArgumentOutOfRangeException) { return new StrokeCollection(); } Prevention
- Clamp percentage inputs once in a shared helper
- Validate persisted settings at load time
- Use the same conversion path as the lasso overload to avoid unit drift
When it happens
Trigger: Calling the rectangle HitTest overload with a negative or >100 percentage, typically a 0-1 fraction passed directly or an unclamped user setting.
Common situations: Same unit-confusion as the lasso overload: normalized confidence values, sliders configured with the wrong range, or arithmetic producing values above 100.
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
- ArgumentOutOfRangeException(nameof(percentageWithinBounds))
- SR.InvalidDiameter
- SR.InvalidDiameter
- ArgumentOutOfRangeException(nameof(percentageWithinLasso))
- ArgumentOutOfRangeException(percentageWithinLasso)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/3cb64a37aa9a59b2.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Ink/StrokeCollection2.cs:151
// Return the resulting collection
return lassoedStrokes;
}
/// <summary>
/// Hit-testing with rectangle
/// </summary>
/// <param name="bounds">hitting rectangle</param>
/// <param name="percentageWithinBounds">the percentage of the stroke that must be within
/// the bounds to be considered hit</param>
/// <returns>collection of strokes found inside the rectangle</returns>
public StrokeCollection HitTest(Rect bounds, int percentageWithinBounds)
{
// Check the input parameters
if ((percentageWithinBounds < 0) || (percentageWithinBounds > 100))
{
throw new System.ArgumentOutOfRangeException(nameof(percentageWithinBounds));
}
if (bounds.IsEmpty)
{
return new StrokeCollection();
}
// Enumerate thru the strokes collect those found within the rectangle.
StrokeCollection hits = new StrokeCollection();
foreach (Stroke stroke in this)
{
// Presharp gives a warning when get methods might deref a null. It's complaining
// here that 'stroke'' could be null, but StrokeCollection never allows nulls to be added
// so this is not possible
if (stroke.HitTest(bounds, percentageWithinBounds))
{
hits.Add(stroke);
}
}View on GitHub (pinned to 81131a70a4)