{"record":{"id":"3cb64a37aa9a59b2","repo":"dotnet/wpf","slug":"argumentoutofrangeexception-percentagewithinbounds","errorCode":null,"errorMessage":"ArgumentOutOfRangeException(percentageWithinBounds)","messagePattern":"ArgumentOutOfRangeException\\(percentageWithinBounds\\)","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Ink/StrokeCollection2.cs","lineNumber":151,"sourceCode":"\n            // Return the resulting collection\n            return lassoedStrokes;\n        }\n\n\n        /// <summary>\n        /// Hit-testing with rectangle\n        /// </summary>\n        /// <param name=\"bounds\">hitting rectangle</param>\n        /// <param name=\"percentageWithinBounds\">the percentage of the stroke that must be within \n        /// the bounds to be considered hit</param>\n        /// <returns>collection of strokes found inside the rectangle</returns>\n        public StrokeCollection HitTest(Rect bounds, int percentageWithinBounds)\n        {\n            // Check the input parameters\n            if ((percentageWithinBounds < 0) || (percentageWithinBounds > 100))\n            {\n                throw new System.ArgumentOutOfRangeException(nameof(percentageWithinBounds));\n            }\n            if (bounds.IsEmpty)\n            {\n                return new StrokeCollection();\n            }\n\n            // Enumerate thru the strokes collect those found within the rectangle.\n            StrokeCollection hits = new StrokeCollection();\n            foreach (Stroke stroke in this)\n            {\n                // Presharp gives a warning when get methods might deref a null.  It's complaining\n                // here that 'stroke'' could be null, but StrokeCollection never allows nulls to be added\n                // so this is not possible\n                if (stroke.HitTest(bounds, percentageWithinBounds))\n                {\n                    hits.Add(stroke);\n                }\n            }","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Ink/StrokeCollection2.cs#L133-L169","documentation":"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.","triggerScenarios":"Calling the rectangle HitTest overload with a negative or >100 percentage, typically a 0-1 fraction passed directly or an unclamped user setting.","commonSituations":"Same unit-confusion as the lasso overload: normalized confidence values, sliders configured with the wrong range, or arithmetic producing values above 100.","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"],"exampleFix":"// before\nvar hits = strokes.HitTest(rect, conf); // conf in 0..1\n// after\nvar hits = strokes.HitTest(rect, Math.Clamp((int)(conf * 100), 0, 100));","handlingStrategy":"validation","validationCode":"int pct = Math.Clamp(percentageWithinBounds, 0, 100);","typeGuard":null,"tryCatchPattern":"try { return strokes.HitTest(bounds, pct); } catch (ArgumentOutOfRangeException) { return new StrokeCollection(); }","preventionTips":["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"],"tags":["wpf","ink","argument-out-of-range","hit-test"],"backgroundTag":"value-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}