Note that there are some explanatory texts on larger screens.

plurals
  1. POstackoverflow in recursion
    primarykey
    data
    text
    <p>i'm making kinda ms paint application, that draws conture and fill inside.I wrote recursive function that fills conture. It works fine ,but if conture is too big program throws stackoverflow exception. How can i solve this problem?? i even can't catch this exception((</p> <pre><code>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 &amp; 0x000000FF), (int)(colorRef &amp; 0x0000FF00) &gt;&gt; 8, (int)(colorRef &amp; 0x00FF0000) &gt;&gt; 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 &amp; 0x00FF0000) &gt;&gt; 16) | (int)(argb &amp; 0x0000FF00) | (int)((argb &amp; 0x000000FF) &lt;&lt; 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 &amp;&amp; PointedColor.G != border.G &amp;&amp; PointedColor.B != border.B &amp;&amp; PointedColor.R != c.R &amp;&amp; PointedColor.G != c.G &amp;&amp; PointedColor.B != c.B &amp;&amp; x &gt;= 0 &amp;&amp; x &lt; panel1.Size.Width &amp;&amp; y &gt;= 0 &amp;&amp; y &lt; 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; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload