Its the weekend again, and that means I get to play with my hobby project.
I\'ve gotten tired of creating test levels by hand, so I thought I\'d take a break from en
Simple function without check the color tolerance
Using:
var img = Image.FromFile("test.png");
img = img.FloodFill(new Point(0, 0), Color.Red);
img.Save("testcomplete.png", ImageFormat.Png);
Main function:
public static Image FloodFill(this Image img, Point pt, Color color)
{
Stack pixels = new Stack();
var targetColor = ((Bitmap)img).GetPixel(pt.X, pt.Y);
pixels.Push(pt);
while (pixels.Count > 0)
{
Point a = pixels.Pop();
if (a.X < img.Width && a.X > -1 && a.Y < img.Height && a.Y > -1)
{
if (((Bitmap)img).GetPixel(a.X, a.Y) == targetColor)
{
((Bitmap)img).SetPixel(a.X, a.Y, color);
pixels.Push(new Point(a.X - 1, a.Y));
pixels.Push(new Point(a.X + 1, a.Y));
pixels.Push(new Point(a.X, a.Y - 1));
pixels.Push(new Point(a.X, a.Y + 1));
}
}
}
return img;
}