private void view_Click(object sender, EventArgs e)
        {
            Point p = view.PointToClient(System.Windows.Forms.Cursor.Position);

            if (radConfig.Checked)
            {
                SetPointDraw(p.X, p.Y);
            }
            if (radPO.Checked)
            {
                int i = listP.Items.Count - 1;
                if (i > 1)
                {
                    myPoint a = (myPoint)listP.Items[i];
                    while ((((myPoint)listP.Items[i]).det == false) && (i != 0))
                    {
                        i--;
                    }
                    myPoint b = (myPoint)listP.Items[i];
                    closeFigure(a.X, a.Y, b.X, b.Y);
                }

                SetSeedPixel(p.X, p.Y);
            }
            view.Refresh();
        }
        private void ADraw(int x, int y, int k)
        {
            myPoint a = new myPoint(x, y, detc), b;

            view.Image = BMP;
            Color    color = Color.Black;
            Graphics g     = Graphics.FromImage(view.Image);

            if (k != 1 || detc)
            {
                if (detc)
                {
                    listP.Items.Add(a);
                    BMP.SetPixel(a.X, a.Y, color);
                    detc = false;
                }
                else
                {
                    b = (myPoint)listP.Items[listP.Items.Count - 1];
                    listP.Items.Add(a);
                    DrLine(b.X, b.Y, a.X, a.Y, color);
                }
            }
            Zag = true;
        }
        private void SetPointDraw(int x, int y)
        {
            int i = listP.Items.Count - 1;
            int k = 0;

            if (i > 0)
            {
                myPoint a = (myPoint)listP.Items[i];
                while ((((myPoint)listP.Items[i]).det == false) && (i != 0))
                {
                    i--;
                }
                myPoint b = (myPoint)listP.Items[i];
                if (x != b.X || y != b.Y)
                {
                    ADraw(x, y, k);
                }
                else
                {
                    closeFigure(a.X, a.Y, b.X, b.Y);
                }
            }
            else
            {
                ADraw(x, y, k);
            }
        }
        private void endP_Click(object sender, EventArgs e)
        {
            int     i = listP.Items.Count - 1;
            myPoint a = (myPoint)listP.Items[i];

            while ((((myPoint)listP.Items[i]).det == false) && (i != 0))
            {
                i--;
            }
            myPoint b = (myPoint)listP.Items[i];

            closeFigure(a.X, a.Y, b.X, b.Y);
        }
        private void SetSeedPixel(int x, int y)
        {
            myPoint a     = new myPoint(x, y, false);
            Color   color = BMP.GetPixel(x, y);

            if (color.A == 0)
            {
                BMP.SetPixel(x, y, Color.Green);
                listCh.Items.Add(a);
            }
            else
            {
                MessageBox.Show("Unable to enter the seed pixel in place of another seed pixel or border!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void handAdd_Click(object sender, EventArgs e)
        {
            int x, y;

            try
            {
                x = int.Parse(edtX.Text);
                y = int.Parse(edtY.Text);
            }
            catch
            {
                if (MessageBox.Show("Invalid input! Coordinates must be only integer values.", "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Cancel)
                {
                    Close();
                }
                return;
            }
            if (radConfig.Checked)
            {
                SetPointDraw(x, y);
            }
            if (radPO.Checked)
            {
                int i = listP.Items.Count - 1;
                if (i > 1)
                {
                    myPoint a = (myPoint)listP.Items[i];
                    while ((((myPoint)listP.Items[i]).det == false) && (i != 0))
                    {
                        i--;
                    }
                    myPoint b = (myPoint)listP.Items[i];
                    closeFigure(a.X, a.Y, b.X, b.Y);
                }

                SetSeedPixel(x, y);
            }
            view.Refresh();
        }
        private void Fill_Click(object sender, EventArgs e)
        {
            Color colorR = Color.Green, colorB = Color.Black, colorP = Color.FromArgb(0);
            int   x, y;
            int   xLeft, xRight;
            int   i, n;
            bool  det1, det2, det3;

            Stack <myPoint> myStack = new Stack <myPoint>();
            myPoint         a;

            while (listCh.Items.Count != 0)
            {
                n = listCh.Items.Count;
                myStack.Push((myPoint)listCh.Items[n - 1]);
                listCh.Items.RemoveAt(n - 1);
            }

            while (myStack.Count != 0)
            {
                a     = myStack.Pop();
                x     = a.X;
                y     = a.Y;
                xLeft = x - 1;
                det3  = false;

                BMP.SetPixel(x, y, colorR);

                while (BMP.GetPixel(xLeft, y).ToArgb() == colorP.ToArgb())
                {
                    det3 = false;
                    BMP.SetPixel(xLeft, y, colorR);
                    xLeft--;
                }

                xLeft++;
                xRight = x + 1;
                while (BMP.GetPixel(xRight, y).ToArgb() == colorP.ToArgb())
                {
                    det3 = false;
                    BMP.SetPixel(xRight, y, colorR);
                    xRight++;
                }

                xRight--;

                if (det3)
                {
                    continue;
                }

                det1 = true; det2 = true;

                for (i = xLeft; i <= xRight; i++)
                {
                    if (det1 && (BMP.GetPixel(i, y - 1).ToArgb() == colorB.ToArgb() || BMP.GetPixel(i, y - 1).ToArgb() == colorR.ToArgb()))
                    {
                        det1 = false;
                    }
                    if (det2 && (BMP.GetPixel(i, y + 1).ToArgb() == colorB.ToArgb() || BMP.GetPixel(i, y + 1).ToArgb() == colorR.ToArgb()))
                    {
                        det2 = false;
                    }
                    if (BMP.GetPixel(i, y - 1).ToArgb() == colorP.ToArgb() && BMP.GetPixel(i - 1, y - 1).ToArgb() == colorB.ToArgb())
                    {
                        a = new myPoint(i, y - 1, false);
                        myStack.Push(a);
                        det1 = false;
                    }
                    if (BMP.GetPixel(i, y - 1).ToArgb() == colorP.ToArgb() && BMP.GetPixel(i + 1, y - 1).ToArgb() == colorB.ToArgb())
                    {
                        a = new myPoint(i, y - 1, false);
                        myStack.Push(a);
                        det1 = false;
                    }
                    if (BMP.GetPixel(i, y + 1).ToArgb() == colorP.ToArgb() && BMP.GetPixel(i - 1, y + 1).ToArgb() == colorB.ToArgb())
                    {
                        a = new myPoint(i, y + 1, false);
                        myStack.Push(a);
                        det2 = false;
                    }
                    if (BMP.GetPixel(i, y + 1).ToArgb() == colorP.ToArgb() && BMP.GetPixel(i + 1, y + 1).ToArgb() == colorB.ToArgb())
                    {
                        a = new myPoint(i, y + 1, false);
                        myStack.Push(a);
                        det2 = false;
                    }
                }

                if (det1)
                {
                    a = new myPoint(x, y - 1, false);
                    myStack.Push(a);
                }

                if (det2)
                {
                    a = new myPoint(x, y + 1, false);
                    myStack.Push(a);
                }

                view.Refresh();
            }
        }