我试了下,没问题,你看我的代码吧,估计你用ArrayList有问题 Private Sub draw_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load p = Me.PictureBox1.Image End Sub Sub check(ByVal p1 As Point) p.SetPixel(p1.X, p1.Y, Color.Black) Dim newp As New Point() newp.X = p1.X + 1 newp.Y = p1.Y If p.GetPixel(newp.X, newp.Y).B = 255 And p.GetPixel(newp.X, newp.Y).R = 255 And p.GetPixel(newp.X, newp.Y).G = 255 Then myStack.Push(newp) End If newp.X = p1.X - 1 newp.Y = p1.Y If p.GetPixel(newp.X, newp.Y).B = 255 And p.GetPixel(newp.X, newp.Y).R = 255 And p.GetPixel(newp.X, newp.Y).G = 255 Then myStack.Push(newp) End If newp.X = p1.X newp.Y = p1.Y - 1 If p.GetPixel(newp.X, newp.Y).B = 255 And p.GetPixel(newp.X, newp.Y).R = 255 And p.GetPixel(newp.X, newp.Y).G = 255 Then myStack.Push(newp) End If newp.X = p1.X newp.Y = p1.Y + 1 If p.GetPixel(newp.X, newp.Y).B = 255 And p.GetPixel(newp.X, newp.Y).R = 255 And p.GetPixel(newp.X, newp.Y).G = 255 Then myStack.Push(newp) End If End Sub Dim myStack As New Stack() Dim p As Bitmap Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click myStack.Push(thispoint)'给定的第一个点 Do While myStack.Count <> 0 check(myStack.Pop) Loop Me.PictureBox1.Refresh() End Sub End Class
mynewpc(水中日月) :按照你的方法我重新试了一下,发现问题的关键在于,我早先的方法中使用了递归函数,而你则用了Do While 循环。改进后,结果运行成功。虽然对于特别大的图形的操作仍然会有些慢,但已基本满足了要求。谢谢你。qqchen79(知秋一叶):你的方法,我考虑了一下,惭愧得很,暂时还不能理解。让我再仔细的想一想,如果可以,你能否再给多一些提示, 谢谢。问题暂不结贴。如有更优化的方法,欢迎讨论。
An unhandled exception of type 'System.StackOverflowException' occurred in system.drawing.dll
的错误。代码如下:
private void fill(Color fc)
{
if (al.Count == 0) //al是ArrayList,充当堆栈角色。
return;
Point p = (Point)al[al.Count-1];
al.RemoveAt(al.Count-1);
theMap.SetPixel(p.X,p.Y,fc); if(p.X - 1 >0)
{
if(theMap.GetPixel(p.X-1,p.Y).ToArgb() == v)
{
al.Add(new Point(p.X-1,p.Y));
}
}
if(p.Y-1 >0)
{
if(theMap.GetPixel(p.X,p.Y-1).ToArgb() == v)
{
al.Add(new Point(p.X,p.Y-1));
}
}
if(p.X+1<theMap.Width)
{
if(theMap.GetPixel(p.X+1,p.Y).ToArgb() == v)
{
al.Add(new Point(p.X+1,p.Y));
}
}
if(p.Y+1 < theMap.Height)
{
if(theMap.GetPixel(p.X,p.Y+1).ToArgb() == v)
{
al.Add(new Point(p.X,p.Y+1));
}
}
fill(fc);
}是不是我应该在程序中做一些什么设置啊? 盼各位继续指教。
Private Sub draw_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
p = Me.PictureBox1.Image
End Sub
Sub check(ByVal p1 As Point)
p.SetPixel(p1.X, p1.Y, Color.Black)
Dim newp As New Point() newp.X = p1.X + 1
newp.Y = p1.Y
If p.GetPixel(newp.X, newp.Y).B = 255 And p.GetPixel(newp.X, newp.Y).R = 255 And p.GetPixel(newp.X, newp.Y).G = 255 Then
myStack.Push(newp)
End If newp.X = p1.X - 1
newp.Y = p1.Y
If p.GetPixel(newp.X, newp.Y).B = 255 And p.GetPixel(newp.X, newp.Y).R = 255 And p.GetPixel(newp.X, newp.Y).G = 255 Then
myStack.Push(newp)
End If newp.X = p1.X
newp.Y = p1.Y - 1
If p.GetPixel(newp.X, newp.Y).B = 255 And p.GetPixel(newp.X, newp.Y).R = 255 And p.GetPixel(newp.X, newp.Y).G = 255 Then
myStack.Push(newp)
End If newp.X = p1.X
newp.Y = p1.Y + 1
If p.GetPixel(newp.X, newp.Y).B = 255 And p.GetPixel(newp.X, newp.Y).R = 255 And p.GetPixel(newp.X, newp.Y).G = 255 Then
myStack.Push(newp)
End If End Sub Dim myStack As New Stack()
Dim p As Bitmap
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myStack.Push(thispoint)'给定的第一个点
Do While myStack.Count <> 0
check(myStack.Pop)
Loop
Me.PictureBox1.Refresh()
End Sub
End Class