Dynamically adding images to pictureboxes. - Programmers Heaven

Howdy, Stranger!

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


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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


  • PsightoplazmPsightoplazm Posts: 332Member ✭✭
    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.