Reputation: 1069
I am currently creating a little paint program for exercise. Right now i'm trying to do the paint bucket tool, or in other words a flood fill. The funny thing is: if the number of pixels which have to be filled is small, everything works fine. If hate number of to filling pixels is higher, it gives me a SO-Exception. Here is my code:
private void FloodFill(Bitmap picture, int x, int y)
{
if (x <= 0 || y <= 0 || x >= DrawingPanel.Width || y >= DrawingPanel.Height)
{
return;
}
if (picture.GetPixel(x, y) != löschFarbe)
{
return;
}
if (picture.GetPixel(x, y) == löschFarbe)
{
picture.SetPixel(x, y, ColorButton.BackColor);
}
FloodFill(picture, x + 1, y);
FloodFill(picture, x, y + 1);
FloodFill(picture, x - 1, y);
FloodFill(picture, x, y - 1);
FloodFill(picture, x + 1, y + 1);
FloodFill(picture, x - 1, y + 1);
FloodFill(picture, x + 1, y - 1);
FloodFill(picture, x - 1, y - 1);
}
"löschFarbe" is the color which is clicked (which will be erased/overwritten with another color)
Error occurring: If i want to fill the complete picture or a big space, I get an error here:
if (picture.GetPixel(x, y) != löschFarbe)
{
return;
}
Anyone knows how I can solve this?
BTW this is a picture of my program:
Upvotes: 2
Views: 4061
Reputation: 9265
Even a moderate sized floodfill will blow your call stack.
Try converting this recursion based method to a stack based method as in this question.
Upvotes: 3