C# picturebox输出的图形一闪就没了

 private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            // 获取用户输入的矩形中心点、长和宽
            double centerX = Convert.ToDouble(textBox1.Text);
            double centerY = Convert.ToDouble(textBox2.Text);
            double width = Convert.ToDouble(textBox3.Text);
            double height = Convert.ToDouble(textBox4.Text);

            // 创建绘图对象
            Graphics g  = pictureBox1.CreateGraphics();
            g.Clear(Color.White);

            // 获取坐标轴原点的坐标
            double originX = pictureBox1.Width / 2.0 ;
            double originY = pictureBox1.Height / 2.0;

            // 绘制坐标轴
            Pen axisPen = new Pen(Color.Black, 2);
            g.DrawLine(axisPen, 0, (float)(pictureBox1.Height / 2.0), pictureBox1.Width, (float)(pictureBox1.Height / 2.0)); // 水平线
            g.DrawLine(axisPen, (float)(pictureBox1.Width / 2.0), pictureBox1.Height, (float)(pictureBox1.Width / 2.0), 0); // 垂直线

            // 绘制箭头
            Point[] arrow = new Point[3];
            arrow[0] = new Point(pictureBox1.Width - 10, pictureBox1.Height / 2 - 5);
            arrow[1] = new Point(pictureBox1.Width, pictureBox1.Height / 2);
            arrow[2] = new Point(pictureBox1.Width - 10, pictureBox1.Height / 2 + 5);
            g.FillPolygon(Brushes.Black, arrow); // 水平箭头
            arrow[0] = new Point(pictureBox1.Width / 2 - 5, 10);
            arrow[1] = new Point(pictureBox1.Width / 2, 0);
            arrow[2] = new Point(pictureBox1.Width / 2 + 5, 10);
            g.FillPolygon(Brushes.Black, arrow); // 垂直箭头

            // 计算矩形的四个顶点坐标
            //centerX = centerX + originX;
            //centerY = centerY + originY;
            double left = centerX - width / 2.0;
            double right = centerX + width / 2.0;
            double top = centerY - height / 2.0;
            double bottom = centerY + height / 2.0;

            // 绘制矩形
            Pen rectPen = new Pen(Color.Red, 2);
            g.DrawRectangle(rectPen, (float)(originX + left), (float)(originY - bottom), (float)width, (float)height);
        }

麻烦各位牛人看一下为什么我输出的图形会一闪就没了

大概效果:

img

Paint事件在重绘控件时发生,将CreateGraphics();绘制的内容重置(覆盖)。
Graphics g = pictureBox1.CreateGraphics();
改为:Graphics g=e.Graphics;

代码:

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            // 获取用户输入的矩形中心点、长和宽
            double centerX = Convert.ToDouble(textBox1.Text);
            double centerY = Convert.ToDouble(textBox2.Text);
            double width = Convert.ToDouble(textBox3.Text);
            double height = Convert.ToDouble(textBox4.Text);

            // 创建绘图对象
            Graphics g = e.Graphics;
            g.Clear(Color.White);

            // 获取坐标轴原点的坐标
            double originX = pictureBox1.Width / 2.0;
            double originY = pictureBox1.Height / 2.0;

            // 绘制坐标轴
            Pen axisPen = new Pen(Color.Black, 2);
            g.DrawLine(axisPen, 0, (float)(pictureBox1.Height / 2.0), pictureBox1.Width, (float)(pictureBox1.Height / 2.0)); // 水平线
            g.DrawLine(axisPen, (float)(pictureBox1.Width / 2.0), pictureBox1.Height, (float)(pictureBox1.Width / 2.0), 0); // 垂直线

            // 绘制箭头
            Point[] arrow = new Point[3];
            arrow[0] = new Point(pictureBox1.Width - 10, pictureBox1.Height / 2 - 5);
            arrow[1] = new Point(pictureBox1.Width, pictureBox1.Height / 2);
            arrow[2] = new Point(pictureBox1.Width - 10, pictureBox1.Height / 2 + 5);
            g.FillPolygon(Brushes.Black, arrow); // 水平箭头
            arrow[0] = new Point(pictureBox1.Width / 2 - 5, 10);
            arrow[1] = new Point(pictureBox1.Width / 2, 0);
            arrow[2] = new Point(pictureBox1.Width / 2 + 5, 10);
            g.FillPolygon(Brushes.Black, arrow); // 垂直箭头

            // 计算矩形的四个顶点坐标
            //centerX = centerX + originX;
            //centerY = centerY + originY;
            double left = centerX - width / 2.0;
            double right = centerX + width / 2.0;
            double top = centerY - height / 2.0;
            double bottom = centerY + height / 2.0;

            // 绘制矩形
            Pen rectPen = new Pen(Color.Red, 2);
            g.DrawRectangle(rectPen, (float)(originX + left), (float)(originY - bottom), (float)width, (float)height);
        }

Graphics g = pictureBox1.CreateGraphics();改为
Graphics g=e.Praphics;
否则g需要dispose