如何擦除位图上的内容

发布于 2024-07-26 11:18:58 字数 921 浏览 11 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梅窗月明清似水 2024-08-02 11:18:58

为什么不在加载新文件时创建一个全新的位图,并在加载时替换当前分配给 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.

冰火雁神 2024-08-02 11:18:58

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.

夏九 2024-08-02 11:18:58

我认为您的意思是清除先前内容的图片框,该内容恰好是图像文件的位图对象。

你已经完成了
新画笔,新位图,从新位图获取新图形画布。

有机会做一个新的 PictureBox 吗?

因为当我的应用程序必须快速而肮脏地重复显示一组图像时,我就是这么做的。 我确信有更好的方法来清除图片框,例如使用刷新()。

foreach ( ... )
{
  pb_Resim = new PictureBox();
  configPictureBoxDimensions();

  ...

}

为什么不先尝试一下refresh()呢? 它应该有效。

foreach ( ... )
{
  pb_Resim = bmp;
  pb_Resim.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().

foreach ( ... )
{
  pb_Resim = new PictureBox();
  configPictureBoxDimensions();

  ...

}

Why don't you try refresh() first? It should work.

foreach ( ... )
{
  pb_Resim = bmp;
  pb_Resim.refresh();

  ...

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文