gpt4 book ai didi

c# - 递归中的stackoverflow

转载 作者:行者123 更新时间:2023-11-30 22:31:25 26 4
gpt4 key购买 nike

我正在制作一种 ms 绘图应用程序,它在内部绘制轮廓和填充。我编写了填充轮廓的递归函数。它工作正常,但如果 conture 太大,程序会抛出 stackoverflow 异常。我怎么解决这个问题??我什至无法捕捉到这个异常((

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

[DllImport( "user32.dll" )]
static extern IntPtr GetDC( IntPtr hWnd );
[DllImport( "user32.dll" )]
static extern int ReleaseDC( IntPtr hWnd, IntPtr hDC );
[DllImport( "gdi32.dll" )]
static extern int GetPixel( IntPtr hDC, int x, int y );
[DllImport( "gdi32.dll" )]
static extern int SetPixel( IntPtr hDC, int x, int y, int color );

static public Color GetPixel( Control control, int x, int y )
{
Color color = Color.Empty;
if (control != null)
{
IntPtr hDC = GetDC( control.Handle );
int colorRef = GetPixel( hDC, x, y );
color = Color.FromArgb(
(int)(colorRef & 0x000000FF),
(int)(colorRef & 0x0000FF00) >> 8,
(int)(colorRef & 0x00FF0000) >> 16 );
ReleaseDC( control.Handle, hDC );
}
return color;
}
static public void SetPixel( Control control, int x, int y, Color color )
{
if (control != null)
{
IntPtr hDC = GetDC( control.Handle );
int argb = color.ToArgb();
int colorRef =
(int)((argb & 0x00FF0000) >> 16) |
(int)(argb & 0x0000FF00) |
(int)((argb & 0x000000FF) << 16);
SetPixel( hDC, x, y, colorRef );
ReleaseDC( control.Handle, hDC );
}
}

int oldX, oldY;
public Form1()
{
InitializeComponent();
}



private void button1_Click(object sender, EventArgs e)
{
Graphics g = panel1.CreateGraphics();
g.Clear(panel1.BackColor);
}
bool paint;

private void Form1_Load(object sender, EventArgs e)
{

}

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
oldX = e.X;
oldY = e.Y;
paint = true;
}

private void panel1_MouseUp(object sender, MouseEventArgs e)
{
paint = false;
}

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (paint)
{
Graphics g = panel1.CreateGraphics();
Pen p = new Pen(Color.Black);
g.DrawLine(p, oldX, oldY, e.X, e.Y);
oldX = e.X;
oldY = e.Y;
}
}

private void panel1_MouseDoubleClick(object sender, MouseEventArgs e)
{
fill(e.X, e.Y, Color.Black, Color.Red);
Color c = GetPixel(panel1, e.X, e.Y);
ClearButton.BackColor = c;
label1.Text = e.X + " " + e.Y;

}
private void fill(int x, int y, Color border, Color c) {

Color PointedColor = GetPixel(panel1, x, y);

try {
if (PointedColor.R != border.R && PointedColor.G != border.G && PointedColor.B != border.B &&
PointedColor.R != c.R && PointedColor.G != c.G && PointedColor.B != c.B &&
x >= 0 && x < panel1.Size.Width && y >= 0 && y < panel1.Size.Height)
{
SetPixel(panel1, x, y, c);

fill(x - 1, y, border, c);
fill(x + 1, y, border, c);
fill(x, y - 1, border, c);
fill(x, y + 1, border, c);

}

}
catch(System.StackOverflowException e)
{
label1.Text = e.Message;
}

}
}
}

最佳答案

您应该使用非递归洪水填充算法。

有关描述,请参阅维基百科 article

Bob Powell 有一些源代码 here .

请注意,试图通过每次都创建一个新线程来解决当前堆栈溢出的问题是一种非常昂贵的解决方案:使用简单的非递归解决方案绝对是更好的方法。创建额外的线程来克服堆栈问题不是对线程的正确使用。如果您认为使用多线程可以获得更好的性能,即使那样您也应该采用非递归解决方案。

关于c# - 递归中的stackoverflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9249943/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com