我有 ac# windows 窗体程序,用户可以在其中用鼠标在图片框中的图像上画线。图形是由 pictureBox1_Paint 方法创建的。如何擦除绘制的线条并保持图像完整?
在这里定义的默认图像:
public lineTest()
{
InitializeComponent();
defaultImage = pictureBox1.Image;
}
画出这样的线条:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
lines.Push(new Line { Start = e.Location });
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (lines.Count > 0 && e.Button == System.Windows.Forms.MouseButtons.Left)
{
lines.Peek().End = e.Location;
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
foreach (var line in lines)
{
Pen magenta = new Pen(Color.Magenta, 5);
e.Graphics.DrawLine(magenta, line.Start, line.End);
}
}
并尝试使用以下方法擦除线条:
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = defaultImage;
pictureBox1.Invalidate();
}
似乎什么也没发生。
四季花海
相关分类