Dynamically adding images to pictureboxes.


I would like to assign images to the dynamically created picture boxes.

For this i would like to dispose the other images which are not currently in the area of our view.

pls provide me solutions in c#.


  • This normally shouldn't be neccessary in C# because .NET's garbage collection will take care of it for you - but if you want to get that warm and fuzzy from disposing an object yourself:

    When you are getting ready to assign the image to your picturebox - first grab the existing picture (if any) and dispose of it after making the assignment.

    var newImage = new Bitmap(100, 100);
    var oldImage = pictureBox1.Image;
    pictureBox1.Image = newImage;

    it is important to not dispose of an image while it is still attached to the picture box because if the picture box control tries to redraw or use the image for anything it will crash.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion