3

我有一个包含图片框控件的 C# winForm 应用程序。此控件有一个 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;
    }
  }
}
4

3 回答 3

2

为什么不在加载新文件时创建一个全新的位图,并在加载时替换当前分配给 pb_Resim.Image 的位图?这将允许 GC 收集旧位图,而无需您花费任何精力“清除”以前的位图,并确保您拥有一个全新的、新鲜的位图,而新加载的文件没有任何类型的残留垃圾。

于 2009-07-01T15:58:24.727 回答
0

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();

  ...

}
于 2009-08-10T05:51:34.560 回答
0

Christian Graus 写了几篇关于 GDI+ 的文章,您可以在CodeProject上找到这些文章。(向下滚动到 GDI+ 文章。)由于它们与您正在执行的活动类型直接相关,因此我建议您仔细阅读它们。

于 2009-07-01T16:17:38.983 回答