直接粘代码,不说废话
public static void floodFillScanLineWithStack(Bitmap map, int x, int y, Color newColor)
{
Color oldColor = map.GetPixel(x, y);
if (oldColor == newColor)
{
return;
}
Stack<int> stackX = new Stack<int>();
Stack<int> stackY = new Stack<int>();
stackX.Clear();
stackY.Clear();
int y1;
bool spanLeft, spanRight;
stackX.Push(x);
stackY.Push(y);
while (stackX.Count > 0 && stackY.Count > 0)
{
x = stackX.Pop();
y = stackY.Pop();
y1 = y;
while (y1 >= 0 && map.GetPixel(x, y1) == oldColor)
{
y1--;
}
y1++;
spanLeft = false;
spanRight = false;
while (y1 < map.Height && map.GetPixel(x, y1) == oldColor)
{
map.SetPixel(x, y1, newColor);
if (!spanLeft && x > 0 && map.GetPixel(x - 1, y1) == oldColor)
{
stackX.Push(x - 1);
stackY.Push(y1);
spanLeft = true;
}
else if (spanLeft && x > 0 && map.GetPixel(x - 1, y1) != oldColor)
{
spanLeft = false;
}
if (!spanRight && x < map.Width - 1 && map.GetPixel(x + 1, y1) == oldColor)
{
stackX.Push(x + 1);
stackY.Push(y1);
spanRight = true;
}
else if (spanRight && x < map.Width - 1 && map.GetPixel(x + 1, y1) != oldColor)
{
spanRight = false;
}
y1++;
}
}
}