Dispose of a pictureBox image without losing the pictureBox

前端 未结 2 526
栀梦
栀梦 2021-01-23 02:01

I am writing a program that will play a slideshow (among other things). The slideshow is controlled by a backgroundWorker, and is set to a while(true) loop so it will constantly

相关标签:
2条回答
  • 2021-01-23 02:14

    What if you store the image to variable of that type and then set your picturebox image and then dispose the older one like

           Image oldImage = horPicBox.Image;
           horPicBox.Image = Image.FromFile(imagePaths[index]);
           oldImage.Dispose();
    
    0 讨论(0)
  • 2021-01-23 02:24

    What is the number of images and total size? I think it is better to load all images in array and assign them to horPicBox than loading them multiple times. To use Dispose first assign horPicBox.Image to temp object, then assign horPicBox.Image to null or next image and call Dispose at the end for the temp object:

    Image img = horPicBox.Image;
    horPicBox.Image = Image.FromFile(imagePaths[index]);
    if ( img != null ) img.Dispose();
    
    0 讨论(0)
提交回复
热议问题