{"record":{"id":"9d0bcd6bd94d21ee","repo":"TheAlgorithms/C-Sharp","slug":"nameof-location-should-point-to-a-pixel-within-the-bitmap","errorCode":null,"errorMessage":"{nameof(location)} should point to a pixel within the bitmap","messagePattern":"(.+?) should point to a pixel within the bitmap","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Algorithms/Other/FloodFill.cs","lineNumber":28,"sourceCode":"/// (description adapted from https://en.wikipedia.org/wiki/Flood_fill)\n/// (see also: https://www.techiedelight.com/flood-fill-algorithm/).\n/// </summary>\npublic static class FloodFill\n{\n    private static readonly List<(int XOffset, int YOffset)> Neighbors = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)];\n\n    /// <summary>\n    /// Implements the flood fill algorithm through a breadth-first approach using a queue.\n    /// </summary>\n    /// <param name=\"bitmap\">The bitmap to which the algorithm is applied.</param>\n    /// <param name=\"location\">The start location on the bitmap.</param>\n    /// <param name=\"targetColor\">The old color to be replaced.</param>\n    /// <param name=\"replacementColor\">The new color to replace the old one.</param>\n    public static void BreadthFirstSearch(SKBitmap bitmap, (int X, int Y) location, SKColor targetColor, SKColor replacementColor)\n    {\n        if (location.X < 0 || location.X >= bitmap.Width || location.Y < 0 || location.Y >= bitmap.Height)\n        {\n            throw new ArgumentOutOfRangeException(nameof(location), $\"{nameof(location)} should point to a pixel within the bitmap\");\n        }\n\n        var queue = new List<(int X, int Y)>\n        {\n            location,\n        };\n\n        while (queue.Count > 0)\n        {\n            BreadthFirstFill(bitmap, location, targetColor, replacementColor, queue);\n        }\n    }\n\n    /// <summary>\n    /// Implements the flood fill algorithm through a depth-first approach through recursion.\n    /// </summary>\n    /// <param name=\"bitmap\">The bitmap to which the algorithm is applied.</param>\n    /// <param name=\"location\">The start location on the bitmap.</param>","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Other/FloodFill.cs#L10-L46","documentation":"FloodFill.BreadthFirstSearch replaces the color connected to a starting pixel, so the start location must be a valid pixel coordinate inside the SKBitmap. It throws ArgumentOutOfRangeException naming 'location' when X or Y is negative or beyond Width/Height.","triggerScenarios":"Calling BreadthFirstSearch with a click coordinate not converted from screen to bitmap space, or with (0,0) on a logically empty/zero-sized bitmap, or coordinates from an image scaled differently than the bitmap.","commonSituations":"Paint-app click handlers using control coordinates instead of client/bitmap coordinates; DPI or zoom scaling not accounted for; empty bitmaps loaded from failed decodes.","solutions":["Validate 0 <= X < bitmap.Width and 0 <= Y < bitmap.Height before calling","Convert mouse coordinates to bitmap coordinates accounting for zoom/DPI","Check bitmap dimensions are non-zero before starting the fill"],"exampleFix":"// before\nFloodFill.BreadthFirstSearch(bitmap, clickPos, oldColor, newColor);\n// after\nif (clickPos.X >= 0 && clickPos.X < bitmap.Width && clickPos.Y >= 0 && clickPos.Y < bitmap.Height)\n    FloodFill.BreadthFirstSearch(bitmap, clickPos, oldColor, newColor);","handlingStrategy":"validation","validationCode":"bool inBounds = location.X >= 0 && location.X < bitmap.Width && location.Y >= 0 && location.Y < bitmap.Height;\nif (!inBounds) throw new ArgumentOutOfRangeException(nameof(location));","typeGuard":"static bool IsInside((int X, int Y) p, SKBitmap b) => p.X >= 0 && p.X < b.Width && p.Y >= 0 && p.Y < b.Height;","tryCatchPattern":"try { FloodFill.BreadthFirstSearch(bitmap, location, target, replacement); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"location\") { /* clamp or ignore click */ }","preventionTips":["Convert screen/click coordinates to bitmap space (zoom, DPI) before filling","Clamp coordinates: Math.Clamp(x, 0, bitmap.Width - 1)","Verify bitmap dimensions are non-zero after decode"],"tags":["argument-out-of-range","coordinates","graphics"],"backgroundTag":"argument-out-of-range","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}