I try to use stack class for paint bucket function ,but when running and clicking on selected area in picture box to be filled with selected color, nothing happens and cpu usage goes up to %100 and system hangs! here is the code,first, picture box code for selecting this function and call fill function after clicking:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (act == "color")
{
fill(bmp, e.X, e.Y, bmp.GetPixel(e.X, e.Y));
pictureBox1.Image = bmp;
}
=================================================
private void fill(Bitmap picture, int x, int y, Color bcolor)
{
if (x > 0 && x < picture.Width && y > 0 && y < picture.Height)
{
Point p = new Point(x, y);
Stack<Point> s = new Stack<Point>();
s.Push(p);
while (s.Count != 0)
{
p = s.Pop();
Color currentcolor = picture.GetPixel(p.X, p.Y);
if (currentcolor == bcolor)
{
picture.SetPixel(p.X, p.Y, currentcolor);
s.Push(new Point(p.X - 1, p.Y));
s.Push(new Point(p.X + 1, p.Y));
s.Push(new Point(p.X, p.Y - 1));
s.Push(new Point(p.X, p.Y + 1));
}
So ,any suggestion for correcting or a better code for paint bucket?