gpt4 book ai didi

c# - 在 C# 中使用鼠标单击在图片框上绘制线条

转载 作者:太空宇宙 更新时间:2023-11-03 20:38:15 27 4
gpt4 key购买 nike

我正在尝试编写一个程序来在 picturebox 上画线使用鼠标单击绘制线的起点和终点的位置。这是我当前的代码:

public partial class Form1 : Form
{
int Drawshape;

private Point p1, p2;
List<Point> p1List = new List<Point>();
List<Point> p2List = new List<Point>();

public Form1()
{
InitializeComponent();
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}

private void button1_Click(object sender, EventArgs e)
{
Drawshape = 1;
}

private void button2_Click(object sender, EventArgs e)
{
Drawshape = 2;
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (Drawshape == 1)
{
if (p1.X == 0)
{
p1.X = e.X;
p1.Y = e.Y;
}
else
{
p2.X = e.X;
p2.Y = e.Y;

p1List.Add(p1);
p2List.Add(p2);

Invalidate();
p1.X = 0;
}
}
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics G = Graphics.FromImage(pictureBox1.Image);
if (Drawshape == 1)
{
using (var p = new Pen(Color.Blue, 4))
{
for (int x = 0; x < p1List.Count; x++)
{
G.DrawLine(p, p1List[x], p2List[x]);
}
}
}
}

目前它根本不允许我在图片框上绘图。这怎么可能?

最佳答案

Invalidate(); 更改为 pictureBox1.Invalidate();

关于c# - 在 C# 中使用鼠标单击在图片框上绘制线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4187524/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com