c#中如何实现点击按钮后在picturebox中绘图

2025-04-14 15:39:14
推荐回答(1个)
回答1:

需要对PictureBox的Pain事件编程

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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        bool b = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            b = true;
            pictureBox1.Refresh();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (b == false) return;
            Graphics g = e.Graphics;
            Pen p = new Pen(Color.Red, 2);
            g.DrawLine(p, 0, 0, 100, 100);
        }
        
    }
}