{"record":{"id":"30499ab01be79033","repo":"dotnet/wpf","slug":"argumentoutofrangeexception-nameof-percentagewithinbounds","errorCode":null,"errorMessage":"ArgumentOutOfRangeException(nameof(percentageWithinBounds))","messagePattern":"ArgumentOutOfRangeException\\(nameof\\(percentageWithinBounds\\)\\)","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Ink/Stroke2.cs","lineNumber":210,"sourceCode":"        {\n            if (Double.IsNaN(diameter) || diameter < DrawingAttributes.MinWidth || diameter > DrawingAttributes.MaxWidth)\n            {\n                throw new ArgumentOutOfRangeException(nameof(diameter), SR.InvalidDiameter);\n            }\n            return HitTest(new Point[]{point}, new EllipseStylusShape(diameter, diameter, TapHitRotation));\n        }\n\n        /// <summary>\n        /// Check whether a certain percentage of the stroke is within the Rect passed in.\n        /// </summary>\n        /// <param name=\"bounds\"></param>\n        /// <param name=\"percentageWithinBounds\"></param>\n        /// <returns></returns>\n        public bool HitTest(Rect bounds, int percentageWithinBounds)\n        {\n            if ((percentageWithinBounds < 0) || (percentageWithinBounds > 100))\n            {\n                throw new System.ArgumentOutOfRangeException(nameof(percentageWithinBounds));\n            }\n\n            if (percentageWithinBounds == 0)\n            {\n                return true;\n            }\n\n            StrokeInfo strokeInfo = null;\n            try\n            {\n                strokeInfo = new StrokeInfo(this);\n\n                StylusPointCollection stylusPoints = strokeInfo.StylusPoints;\n                double target = strokeInfo.TotalWeight * percentageWithinBounds / 100.0f - PercentageTolerance;\n\n                for (int i = 0; i < stylusPoints.Count; i++)\n                {\n                    if (bounds.Contains((Point)stylusPoints[i]))","sourceCodeStart":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Ink/Stroke2.cs#L192-L228","documentation":"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.","triggerScenarios":"Passing a negative percentage or a value greater than 100, often from an unclamped slider value, parsed user input, or a bad config constant.","commonSituations":"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).","solutions":["Clamp the value: Math.Clamp(percentageWithinBounds, 0, 100) before calling","Validate user/config input at load time and reject or normalize values outside 0-100","Use Math.Round on computed percentages and verify the range"],"exampleFix":"// before\nbool hit = stroke.HitTest(bounds, percentage);\n// after\nint pct = Math.Clamp(percentage, 0, 100);\nbool hit = stroke.HitTest(bounds, pct);","handlingStrategy":"validation","validationCode":"bool valid = percentageWithinBounds is >= 0 and <= 100;","typeGuard":"static bool IsValidPercentage(int p) => p is >= 0 and <= 100;","tryCatchPattern":"try { hit = stroke.HitTest(bounds, pct); } catch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"percentageWithinBounds\") { hit = stroke.HitTest(bounds, Math.Clamp(pct, 0, 100)); }","preventionTips":["Clamp percentages to 0-100 at input boundaries","Validate config values for selection sensitivity","Use range-checked properties instead of raw ints"],"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"}