C#实现种子填充算法的过程出现了一点小问题

在用C#写种子填充算法的时候,选完种子点后进行填充时只能绘出一条直线,还不会停止
下面是我的全部代码,没有报错
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static bool huitu = false;
        public static bool xuandian = false;
        public static bool blnDraw = false;
        Point start;
        Point temp;
        Rectangle rect;

         private void button1_Click(object sender, EventArgs e)
         {
            Graphics g1 = this.CreateGraphics();
            g1.Clear(Color.White);
            huitu = true;
            g1.Dispose();
         }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (huitu)
            {
                start = e.Location;
                Invalidate();
                blnDraw = true;
            }
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (!huitu)
                return;
            if (blnDraw)
            {
                if (e.Button != MouseButtons.Left)
                    return;
                Point tempEndPoint = e.Location;
                rect.Location = new Point(Math.Min(start.X, tempEndPoint.X), Math.Min(start.Y, tempEndPoint.Y));
                rect.Size = new Size(Math.Abs(start.X - tempEndPoint.X), Math.Abs(start.Y - tempEndPoint.Y));
                this.Invalidate();
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            blnDraw = false;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (blnDraw)
            {
                if (rect != null && rect.Width > 0 && rect.Height > 0)
                {
                    e.Graphics.DrawRectangle(new Pen(Color.Black, 3), rect);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {    
            BoundaryFill(temp.X, temp.Y, Color.FromArgb(0, 0, 0), Color.FromArgb(255, 0, 0));//边界种子填充算法
        }

        /// 
        /// 种子填充算法(边界填充)
        /// 
        /// 横坐标
        /// 纵坐标
        /// 边界颜色
        /// 填充颜色
        void BoundaryFill(int x, int y, Color boundarycolor, Color newcolor)
        {
            Graphics g1 = this.CreateGraphics();
            //判断是否为边界颜色或者新的颜色
            //默认边界颜色为黑色
            Color c;
            c = GetColor(x, y);
            if ((c != boundarycolor) && (c != newcolor))
            {
                g1.FillRectangle(Brushes.Red, x - 1, y - 1, 2, 2);
                BoundaryFill(x + 1, y, boundarycolor, newcolor);
                BoundaryFill(x - 1, y, boundarycolor, newcolor);
                BoundaryFill(x, y + 1, boundarycolor, newcolor);
                BoundaryFill(x, y - 1, boundarycolor, newcolor);
                x++;
                y++;
            }
        }

        /// 
        /// 获取指定窗口的设备场景
        /// 
        /// 将获取其设备场景的窗口的句柄。若为0,则要获取整个屏幕的DC
        /// 指定窗口的设备场景句柄,出错则为0
        [DllImport("user32.dll")]
        public static extern IntPtr GetDC(IntPtr hwnd);

        /// 
        /// 释放由调用GetDC函数获取的指定设备场景
        /// 
        /// 要释放的设备场景相关的窗口句柄
        /// 要释放的设备场景句柄
        /// 执行成功为1,否则为0
        [DllImport("user32.dll")]
        public static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);

        /// 
        /// 在指定的设备场景中取得一个像素的RGB值
        /// 
        /// 一个设备场景的句柄
        /// 逻辑坐标中要检查的横坐标
        /// 逻辑坐标中要检查的纵坐标
        /// 指定点的颜色
        [DllImport("gdi32.dll")]
        public static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);

        /// 
        /// 获取当前像素点的颜色
        /// 
        /// 横坐标
        /// 纵坐标
        /// 
        public Color GetColor(int x, int y)
        {
            IntPtr hdc = GetDC(IntPtr.Zero); 
            uint pixel = GetPixel(hdc, x, y);
            ReleaseDC(IntPtr.Zero, hdc);
            Color color = Color.FromArgb((int)(pixel & 0x000000FF), (int)(pixel & 0x0000FF00) >> 8, (int)(pixel & 0x00FF0000) >> 16);
            return color;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            huitu = false;
            xuandian = true;
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            if (!xuandian)
                return;
            temp = e.Location;
            Graphics g1 = this.CreateGraphics();
            g1.FillRectangle(Brushes.Red, temp.X - 1, temp.Y - 1, 2, 2);
        }
    }
}
结果图片

img

我搜到了各种种子算法填充,但都是在bitmap上做的,和我这个不太一样

我改怎么改啊

试试改一下if语句判断的值,或者xy坐标的参数(因为我也不懂这方面的太多),我以前在写二维数组的程序时,一直停不下来,发现是参数错了。