为了账号安全,请及时绑定邮箱和手机立即绑定

在c# windows窗体中清除画在图片框上的线条

在c# windows窗体中清除画在图片框上的线条

C#
慕田峪7331174 2022-01-09 16:22:21
我有 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();    }似乎什么也没发生。
查看完整描述

1 回答

?
四季花海

TA贡献1811条经验 获得超5个赞

每次使控件无效时都会调用 Paint,因此每次都会重新绘制线条。在您的button1_Click事件处理程序中添加以下行:

lines.Clear();

在你打电话之前 pictureBox1.Invalidate();

这将在下次触发绘制事件时停止重新绘制线条。


查看完整回答
反对 回复 2022-01-09
  • 1 回答
  • 0 关注
  • 877 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号