Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7495079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:58:48+00:00 2026-05-29T17:58:48+00:00

i’m making kinda ms paint application, that draws conture and fill inside.I wrote recursive

  • 0

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((

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;
            }

        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:58:48+00:00Added an answer on May 29, 2026 at 5:58 pm

    You are not allowed to catch a StackOverflowException by design:

    Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default. Consequently, users are advised to write their code to detect and prevent a stack overflow. For example, if your application depends on recursion, use a counter or a state condition to terminate the recursive loop.

    I’m sure there are more efficient ways of achieving this. However, to start you off, you can convert your recursion to an iteration by reifying the call stack as a Stack<T>:

    private void fill(int xInitial, int yInitial, Color border, Color c) 
    {
        var remaining = new Stack<Tuple<int, int>>();
        remaining.Push(Tuple.Create(xInitial, yInitial));
    
        while (remaining.Any())
        {
            var next = remaining.Pop();
            int x = next.Item1;
            int y = next.Item2;
    
            Color PointedColor = GetPixel(panel1, x, y);
    
            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);
                remaining.Push(Tuple.Create(x - 1, y));
                remaining.Push(Tuple.Create(x + 1, y));
                remaining.Push(Tuple.Create(x, y - 1));
                remaining.Push(Tuple.Create(x, y + 1));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.