How to erase content in a bitmap

I have a C # winForm application that contains a pictureBox control. This control has a Paint event. Every time a drawing event occurs, a bitmap is dynamically created, and I perform a drawing on it. And when the user clicks the "Save" button, the edited image is saved as a jpg file.

This is normal so far. When I upload a new image in the PictureBox control, the remains of previous changes are still alive.

How to erase a bitmap and start it every time you upload 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;
    }
  }
}
+3
source share
3 answers

, pb_Resim.Image ? GC, - "" , , - .

+2

GDI +, CodeProject. ( GDI +.) , , .

0

, , .

You have already made a new brush, a new bitmap, getting a new graphic canvas from a new bitmap.

Is there a chance to make a new PictureBox?

Because what I did when my application displayed a lot of images quickly and dirty. I am sure there is a better way to clean the camera, for example using refresh ().

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

  ...

}

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

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

  ...

}
0
source

Source: https://habr.com/ru/post/1711676/


All Articles