Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6996085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:06:41+00:00 2026-05-27T20:06:41+00:00

How can i dispose all the images in a generic list in WPF? Here

  • 0

How can i dispose all the images in a generic list in WPF?

Here is my try:

//piclist is a global variable pointing to a folder on harddrive 
foreach (string s in this.piclist)  
{
   this.picsToDisplay.Add(this.BitmapFromUri(new Uri(s)));
}

private BitmapImage LoadImage(string myImageFile)
    {
        BitmapImage myRetVal = null;
        if (myImageFile != null)
        {
            BitmapImage image = new BitmapImage();
            using (FileStream stream = File.OpenRead(myImageFile))
            {
                image.BeginInit();
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.StreamSource = stream;
                image.EndInit();
            }
            myRetVal = image;
        }
        return myRetVal;
    }

and this is how i clear and dispose everything:

if (this.picsToDisplay.Count > 0)
        {
            foreach (BitmapImage b in this.picsToDisplay)
                b.StreamSource.Dispose();

            this.picsToDisplay.Clear();
        }

        Array.ForEach(Directory.GetFiles(this.tempPics),
                        delegate(string path) { File.Delete(path); });

It crashes first at b.StreamSource.Dispose();
Saying that streamsource is null ‘reference not set to an instance of an object’

If i comment out that line, it crashes at this part: File.Delete(path);
With the message that the picture i’m trying to delete is in use.

The pictures getting displayed in an image control in WPF.
Its source is already set to NULL before i proceed to dispose etc.

What am i missing guys?

============
EDIT

private void btnLoadPictures_Click(object sender, EventArgs e)
{
    this.ClearPicturesFromList();
    DirectoryInfo Dir = new DirectoryInfo(this.pictures);
    List<string> stringList = new List<string>();
    FileInfo[] picList = Dir.GetFiles();

    stringList = (from FI in picList
         where FI.LastWriteTime.Date.ToString("dd-MM-yyyy") == 
         this.dpPictureDate.SelectedDate.Value.ToString("dd-MM-yyyy")
         select (FI.FullName)).ToList();

try
{
if (Directory.Exists(this.tempPics))
{
    if (stringList.Count > 0)
    {
        foreach (string s in stringList)
        {
            string destFolder = System.IO.Path.Combine(this.tempPics, System.IO.Path.GetFileName(s));
            File.Copy(s, destFolder, true);
        }

        DirectoryInfo Dir2 = new DirectoryInfo(this.tempPics);
        FileInfo[] pics = Dir2.GetFiles();

        this.picsInTempFolder = (from FI in pics
                            select (FI.FullName)).ToList();

        foreach (string s in this.picsInTempFolder)
        {
            this.picsToDisplay.Add(this.LoadImage(s));
        }

        this.indexCounter = 0;
        this.imgBox.Source = (BitmapImage)this.picsToDisplay[this.indexCounter];
        this.tbxPictureName.Text = System.IO.Path.GetFileName(this.picsInTempFolder[this.indexCounter]);
        stringList.Clear();
        pics = null;
        }
    }
}

catch (Exception exp)
{
    this.WriteToRichTextBox(exp.ToString());
}

}

pressing the clearimages button for the first time, i get the exception about the file being in use. doing it the second time, actually works fine.

putting a thread.sleep between clearing all lists and stuff than delete the files in the temp directory doenst work. but somehow it needs a sort of delay.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:06:42+00:00Added an answer on May 27, 2026 at 8:06 pm

    try this method for loading images

     private BitmapImage LoadImage(string myImageFile)
            {
                BitmapImage myRetVal = null;
                if (myImageFile != null)
                {
                    BitmapImage image = new BitmapImage();
                    using (FileStream stream = File.OpenRead(myImageFile))
                    {
                        image.BeginInit();
                        image.CacheOption = BitmapCacheOption.OnLoad;
                        image.StreamSource = stream;
                        image.EndInit();
                    }
                    myRetVal = image;
                }
                return myRetVal;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have 10 List<Car> . Is there a way I can 'dispose' CarInstance1
I have checked all posts here, but can't find a solution for me so
How can I ensure the following code is disposing of all objects in a
I have an array of images named image_<somenumber>_trans.png All these images have transparent areas.
I'm getting an OutOfMemory Exception when trying to resize larger images. Our server can
Is there a way to dispose method variables without using try-finally block in C#
Is it necessary to set to Nothing (in Dispose()) all WithEvents fields? Apparently Handles
How can I make sure in the following code snippet that IDataReader is disposed
can anyone help me in trying to check whether JavaScript is enabled in client
Can 2 or more equations defining a function in Haskell share the same where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.