views:

1263

answers:

3

Hi 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.

But 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.

Here is the code snippet for PaintEvet

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;
        }
    }
}

Thanks in advance

+1  A: 

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.

jrista
A: 

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.

John Fisher
A: 

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

  ...

}
Blessed Geek