Wq_wn 2023-03-27 11:38 采纳率: 90.9%
浏览 51
已结题

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

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

  • 写回答

2条回答 默认 最新

  • 夜飞鼠 2023-03-27 12:30
    关注

    大概效果:

    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);
            }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月30日
  • 已采纳回答 3月27日
  • 创建了问题 3月27日

悬赏问题

  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗