如何擦除位图上的内容
我有 C# winForm 应用程序,它包含一个 pictureBox 控件。 该控件有一个 Paint 事件。 每次触发绘制事件时,都会动态创建一个位图,并在其上执行一些绘制。 当用户单击“保存”按钮时,编辑的图像将保存为 jpg 文件。
到现在为止都还好。 当我在 pictureBox 控件中加载新图像时,之前编辑的内容仍然有效。
如何删除位图并在每次加载新图像时重新开始:
private void pb_Resim_Paint(object sender, PaintEventArgs e)
{
List<eVucutParcalari> list = new List<eVucutParcalari>(pointList.Keys);
// Loop through list
foreach (eVucutParcalari k in list)
{
Dictionary<Point, Color> dicItem = pointList[k];
foreach (KeyValuePair<Point, Color> pair in dicItem)
{
Point p = pair.Key;
Color c = pair.Value;
SolidBrush brush = new SolidBrush(c);
if (pb_Resim.Image == null)
return;
Bitmap bmp = new Bitmap(pb_Resim.Image);
Graphics gr = Graphics.FromImage(bmp);
gr.FillRectangle(brush, p.X, p.Y, 5, 5);
pb_Resim.Image = bmp;
}
}
}
I have C# winForm application which holds a pictureBox control. This control has a Paint event. Every time paint event is fired , a Bitmap is dynamically created and I perform some drawing on it. And when user clicks "save" button , edited image is saved as jpg file.
It's OK until now. When I load a new image in pictureBox control remains of previous edits are still alive.
How can I erase the bitmap and start fresh each time I load a new image:
private void pb_Resim_Paint(object sender, PaintEventArgs e)
{
List<eVucutParcalari> list = new List<eVucutParcalari>(pointList.Keys);
// Loop through list
foreach (eVucutParcalari k in list)
{
Dictionary<Point, Color> dicItem = pointList[k];
foreach (KeyValuePair<Point, Color> pair in dicItem)
{
Point p = pair.Key;
Color c = pair.Value;
SolidBrush brush = new SolidBrush(c);
if (pb_Resim.Image == null)
return;
Bitmap bmp = new Bitmap(pb_Resim.Image);
Graphics gr = Graphics.FromImage(bmp);
gr.FillRectangle(brush, p.X, p.Y, 5, 5);
pb_Resim.Image = bmp;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不在加载新文件时创建一个全新的位图,并在加载时替换当前分配给 pb_Resim.Image 的位图? 这将允许 GC 收集旧位图,而无需您花费任何精力“清除”以前的位图,并确保您拥有一个全新的、新鲜的位图,而新加载的文件没有任何类型的残留垃圾。
Why not create a brand new bitmap when you load a new file, and replace the bitmap currently assigned to pb_Resim.Image on load? This would allow the old bitmap to be collected by the GC without requiring you to expend any effort "clearing" the previous bitmap, and ensure that you have a brand new, fresh bitmap without any residual junk of any kind for the newly loaded file.
Christian Graus 写了几篇关于 GDI+ 的文章,您可以在 CodeProject。 (向下滚动到 GDI+ 文章。)由于它们与您正在进行的活动类型直接相关,因此我建议仔细阅读它们。
Christian Graus wrote several articles on GDI+, which you can find on CodeProject. (Scroll down to the GDI+ articles.) Since they directly relate to the type of activity you are doing, I suggest looking through them.
我认为您的意思是清除先前内容的图片框,该内容恰好是图像文件的位图对象。
你已经完成了
新画笔,新位图,从新位图获取新图形画布。
有机会做一个新的 PictureBox 吗?
因为当我的应用程序必须快速而肮脏地重复显示一组图像时,我就是这么做的。 我确信有更好的方法来清除图片框,例如使用刷新()。
为什么不先尝试一下refresh()呢? 它应该有效。
I think you meant clear the picture box of the previous content which happens to be the bitmap object of an image file.
You have already done
new brush, new bitmap, getting new graphics canvas from the new bitmap.
Any chance of doing a new PictureBox?
Because that's what I did when my app had to quick-and-dirty repeatedly display a set of images. I'm sure there's a better way to clear the picturebox, like using refresh().
Why don't you try refresh() first? It should work.