TheAlgorithms/C-Sharp · error · ArgumentOutOfRangeException

should point to a pixel within the bitmap

Error message

{nameof(location)} should point to a pixel within the bitmap

What it means

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.

Solutions

  1. Validate 0 <= X < bitmap.Width and 0 <= Y < bitmap.Height before calling
  2. Convert mouse coordinates to bitmap coordinates accounting for zoom/DPI
  3. Check bitmap dimensions are non-zero before starting the fill

Example fix

// before
FloodFill.BreadthFirstSearch(bitmap, clickPos, oldColor, newColor);
// after
if (clickPos.X >= 0 && clickPos.X < bitmap.Width && clickPos.Y >= 0 && clickPos.Y < bitmap.Height)
    FloodFill.BreadthFirstSearch(bitmap, clickPos, oldColor, newColor);
Defensive patterns

Strategy: validation

Validate before calling

bool inBounds = location.X >= 0 && location.X < bitmap.Width && location.Y >= 0 && location.Y < bitmap.Height;
if (!inBounds) throw new ArgumentOutOfRangeException(nameof(location));

Type guard

static bool IsInside((int X, int Y) p, SKBitmap b) => p.X >= 0 && p.X < b.Width && p.Y >= 0 && p.Y < b.Height;

Try / catch

try { FloodFill.BreadthFirstSearch(bitmap, location, target, replacement); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "location") { /* clamp or ignore click */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/9d0bcd6bd94d21ee. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Other/FloodFill.cs:28

/// (description adapted from https://en.wikipedia.org/wiki/Flood_fill)
/// (see also: https://www.techiedelight.com/flood-fill-algorithm/).
/// </summary>
public static class FloodFill
{
    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)];

    /// <summary>
    /// Implements the flood fill algorithm through a breadth-first approach using a queue.
    /// </summary>
    /// <param name="bitmap">The bitmap to which the algorithm is applied.</param>
    /// <param name="location">The start location on the bitmap.</param>
    /// <param name="targetColor">The old color to be replaced.</param>
    /// <param name="replacementColor">The new color to replace the old one.</param>
    public static void BreadthFirstSearch(SKBitmap bitmap, (int X, int Y) location, SKColor targetColor, SKColor replacementColor)
    {
        if (location.X < 0 || location.X >= bitmap.Width || location.Y < 0 || location.Y >= bitmap.Height)
        {
            throw new ArgumentOutOfRangeException(nameof(location), $"{nameof(location)} should point to a pixel within the bitmap");
        }

        var queue = new List<(int X, int Y)>
        {
            location,
        };

        while (queue.Count > 0)
        {
            BreadthFirstFill(bitmap, location, targetColor, replacementColor, queue);
        }
    }

    /// <summary>
    /// Implements the flood fill algorithm through a depth-first approach through recursion.
    /// </summary>
    /// <param name="bitmap">The bitmap to which the algorithm is applied.</param>
    /// <param name="location">The start location on the bitmap.</param>

View on GitHub (pinned to 96e2905cab)