Skip to content

Paint Bucket function code in C# ,hangs when running

Bardia Hakhamanesh

Asked Jan 19, 2016, 8:05 AM EST1 answer
Share
LinkedInX
")
{

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?

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Maybe your code hangs because the stack could be growing infinitely. Try to debug it and see if it never stops growing. To do that, put a counter inside the while loop and keep increasing it with every loop. If the counter exceeds the total size of the image (width * height), that could mean your loop never finishes.

    Anyway there's some other approach to implementing Paint Bucket in this post:
    http://support.leadtools.com/SupportPortal/CS/forums/44770/ShowPost.aspx
    0