我使用Windows窗体应用程序Visual studio Bitmap制作了几张图纸,我的目标是在按下按钮时清除所有图纸(使背景清晰) 以下代码不完全是我的原始代码,但这是我创建每个绘图的方式
Rectangle area;
Bitmap creature;//this only one drawing but I have several
Graphics scG;
Bitmap background;
private void Form1_Load(object sender, EventArgs e)
{
background = new Bitmap(Width, Height);
area = new Rectangle(50, 50, 50, 50);
creature = new Bitmap(@"C:\Users\Desktop\Image.png");
}
public Bitmap Draw()
{
Graphics scG = Graphics.FromImage(background);
scG.DrawImage(creature, area);
return background;
}
}
private void Button_Click(object sender, EventArgs e)
{
// I want to clear all the drawings by push this button
}
答案 0 :(得分:1)
无法删除位图上绘制的内容。
为什么不直接转储旧的background
位图并创建一个新位图?
答案 1 :(得分:0)
真正的方法是使用事件Paint:
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(creature, area); // draw your image here...
}
要删除,只需在Form1_Paint中执行任何操作,并调用Refresh();
答案 2 :(得分:0)
这是重复的,请看这个链接: How to delete a drawn circle in c# windows form?
或测试这些代码:
Graphics.Clear();
或
Control.Invalidate();
或
this.Invalidate();