我要写一个画图软件,在做“填充工具”的时候遇到了问题,一旦填充大图形就会出现System.StackOverflowException的错误,高手帮帮忙,我是用递归写的,有没有不用递归或者更好的方法,先谢谢啦。        private void FindFill(Point Start, ref Image image, Color Fill)
        {
            Bitmap b = (Bitmap)image;
            b.SetPixel(Start.X, Start.Y, Fill);            if (psl == 0) goto cl;
            for (int i = 0; i < psl + 1; i++)
            {
                if (Start == ps[i]) return;
            }        cl: { }
            ps[psl] = new Point(Start.X, Start.Y);
            psl++;            if (Start.X == 0 || Start.Y == 0) return;
            if (Start.X == picImage.Width - 1 || Start.Y == picImage.Height - 1) return;            if (b.GetPixel(Start.X, Start.Y - 1).Name == BaseColor)
            {
                FindFill(new Point(Start.X, Start.Y - 1), ref image, Fill);
            }
            if (b.GetPixel(Start.X, Start.Y + 1).Name == BaseColor)
            {
                FindFill(new Point(Start.X, Start.Y + 1), ref image, Fill);
            }
            if (b.GetPixel(Start.X - 1, Start.Y).Name == BaseColor)
            {
                FindFill(new Point(Start.X - 1, Start.Y), ref image, Fill);
            }
            if (b.GetPixel(Start.X + 1, Start.Y).Name == BaseColor)
            {
                FindFill(new Point(Start.X + 1, Start.Y), ref image, Fill);
            }
        } 

解决方案 »

  1.   

    为什么不直接调用 Graphics.FillPath()方法呢? 
      

  2.   

    Public Sub SafeFloodFill(ByVal bm As Bitmap, ByVal x As _
        Integer, ByVal y As Integer, ByVal new_color As Color)
        ' Get the old and new colors.
        Dim old_color As Color = bm.GetPixel(x, y)    ' Start with the original point in the stack.
        Dim pts As New Stack(1000)
        pts.Push(New Point(x, y))
        bm.SetPixel(x, y, new_color)    ' While the stack is not empty, process a point.
        Do While pts.Count > 0
            Dim pt As Point = DirectCast(pts.Pop(), Point)
            If pt.X > 0 Then SafeCheckPoint(bm, pts, pt.X - 1, _
                pt.Y, old_color, new_color)
            If pt.Y > 0 Then SafeCheckPoint(bm, pts, pt.X, pt.Y _
                - 1, old_color, new_color)
            If pt.X < bm.Width - 1 Then SafeCheckPoint(bm, pts, _
                pt.X + 1, pt.Y, old_color, new_color)
            If pt.Y < bm.Height - 1 Then SafeCheckPoint(bm, _
                pts, pt.X, pt.Y + 1, old_color, new_color)
        Loop
    End Sub' See if this point should be added to the stack.
    Private Sub SafeCheckPoint(ByVal bm As Bitmap, ByVal pts As _
        Stack, ByVal x As Integer, ByVal y As Integer, ByVal _
        old_color As Color, ByVal new_color As Color)
        Dim clr As Color = bm.GetPixel(x, y)
        If clr.Equals(old_color) Then
            pts.Push(New Point(x, y))
            bm.SetPixel(x, y, new_color)
        End If
    End Sub自己翻译下。。如果不行明天给你写个BitmapData版本的。
      

  3.   

    如果这样的话就只能填充5个点,在SafeCheckPoint里面加入递归的话还是会出现System.StackOverflowException错误
      

  4.   

    参考http://blog.csdn.net/zgke/archive/2009/01/21/3847525.aspx