static int unionBlockId = 0;
private UnionBlock FloodPick(Block[][] blockMatrix, Block block, UnionBlock unionBlock)
{
Point location = block.Point;
int width = blockMatrix.Length;
int height = blockMatrix.Length; if (location.X < 0 || location.X >= width || location.Y < 0 || location.Y >= height) return null;
if (block.Visited) return null;
if (!matrixExcludePattern[block.Point.X][block.Point.Y]) return null;
if (unionBlock == null)
{
unionBlock = new UnionBlock();
unionBlock.Id = unionBlockId;
}
else if (block.UnionBlock == unionBlock)
{
return null;
} Stack<Point> points = new Stack<Point>();
points.Push(location);
int ww = width - 1;
int hh = height - 1;
while (points.Count > 0) {
Point p = points.Pop();
if (blockMatrix[p.X][p.Y].IsStuffed && !blockMatrix[p.X][p.Y].Visited)
{
blockMatrix[p.X][p.Y].UnionBlock = unionBlock;
unionBlock.PointsList.Add(p);
blockMatrix[p.X][p.Y].Visited = true;
} if (p.X > 0 && blockMatrix[p.X - 1][p.Y].IsStuffed && !blockMatrix[p.X - 1][p.Y].Visited)
{
blockMatrix[p.X - 1][p.Y].UnionBlock = unionBlock;
unionBlock.PointsList.Add(new Point(p.X - 1,p.Y));
blockMatrix[p.X - 1][p.Y].Visited = true; points.Push(new Point(p.X - 1, p.Y));
}
if (p.X < ww && blockMatrix[p.X + 1][p.Y].IsStuffed && !blockMatrix[p.X + 1][p.Y].Visited)
{
blockMatrix[p.X + 1][p.Y].UnionBlock = unionBlock;
unionBlock.PointsList.Add(new Point(p.X + 1,p.Y ));
blockMatrix[p.X + 1][p.Y].Visited = true; points.Push(new Point(p.X + 1, p.Y));
}
if (p.Y > 0 && blockMatrix[p.X][p.Y - 1].IsStuffed && !blockMatrix[p.X][p.Y - 1].Visited)
{
blockMatrix[p.X][p.Y - 1].UnionBlock = unionBlock;
unionBlock.PointsList.Add(new Point(p.X, p.Y-1));
blockMatrix[p.X][p.Y - 1].Visited = true; points.Push(new Point(p.X, p.Y - 1));
}
if (p.Y < hh && blockMatrix[p.X][p.Y + 1].IsStuffed && !blockMatrix[p.X][p.Y + 1].Visited)
{
blockMatrix[p.X][p.Y + 1].UnionBlock = unionBlock;
unionBlock.PointsList.Add(new Point(p.X, p.Y+1));
blockMatrix[p.X][p.Y + 1].Visited = true; points.Push(new Point(p.X, p.Y + 1));
}
}
unionBlockId++;
return unionBlock;
}

 

 还有更简化的递归调用:

void floodFill(int x, int y, int fill, int
{
if ((x < 0) || (x >= width)) return;
if ((y < 0) || (y >= height)) return;
if
  {
    setPixel(fill, x, y);
    floodFill(x+1, y, fill, old);
    floodFill(x, y+1, fill, old);
    floodFill(x-1, y, fill, old);
    floodFill(x, y-1, fill, old);
  }
}

void BoundaryFill4(int x, int y, long FilledColor, long BoundaryColor)
{
long CurrentColor;
CurrentColor = GetPixelColor(x,y);
if (CurrentColor != BoundaryColor && CurrentColor != FilledColor)
{
SetColor(FilledColor);
SetPixel (x,y);
BoundaryFill4(x+1, y, FilledColor, BoundaryColor);
BoundaryFill4(x-1, y, FilledColor, BoundaryColor);
BoundaryFill4(x, y+1, FilledColor, BoundaryColor);
BoundaryFill4(x, y-1, FilledColor, BoundaryColor);
}
}


左右扫描方式:

void FillRight(int x, int y, ref Bitmap img)
{
if (x < img.Width && y < img.Height && x>0 && y>0)
{
if (img.GetPixel(x, y) != Color.White && img.GetPixel(x, y) != Color.Red)
{
img.SetPixel(x, y, Color.Red);
FillRight(++x, y, ref img);
x = x - 1;
FillRight(x, y - 1, ref img);
FillRight(x, y + 1, ref img);
}
}
}

void FillLeft(int x, int y, ref Bitmap img)
{
if (x < img.Width && y < img.Height && x > 0 && y > 0)
{
if (img.GetPixel(x, y) != Color.White && img.GetPixel(x, y) != Color.Red)
{
img.SetPixel(x, y, Color.Red);
FillLeft(++x, y, ref img);
x = x - 1;
FillLeft(x, y - 1, ref img);
FillLeft(x, y + 1, ref img);
}
}
}