C# 正在处理位图图像的常规列表

C# 正在处理位图图像的常规列表,c#,wpf,image,dispose,bitmapimage,C#,Wpf,Image,Dispose,Bitmapimage,如何在WPF中处理通用列表中的所有图像 以下是我的尝试: //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)

如何在WPF中处理通用列表中的所有图像

以下是我的尝试:

//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;
    }
这就是我清理和处置一切的方式:

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); });
它首先在
b.StreamSource.Dispose()处崩溃
表示streamsource为null“引用未设置为对象的实例”

如果我注释掉那一行,它会在这一部分崩溃:
File.Delete(path)
带有我试图删除的图片正在使用的消息

在WPF中的图像控件中显示的图片。 在我继续处理等之前,它的源已经设置为NULL

我错过了什么

============ 编辑

private void btnLoadPictures\u单击(对象发送者,事件参数e)
{
这是.ClearPicturesFromList();
DirectoryInfo Dir=新的DirectoryInfo(this.pictures);
List stringList=新列表();
FileInfo[]picList=Dir.GetFiles();
stringList=(来自picList中的FI)
其中FI.LastWriteTime.Date.ToString(“dd-MM-yyyy”)==
this.dpPictureDate.SelectedDate.Value.ToString(“dd-MM-yyyy”)
选择(FI.FullName)).ToList();
尝试
{
if(Directory.Exists(this.tempPics))
{
如果(stringList.Count>0)
{
foreach(stringList中的字符串s)
{
string destFolder=System.IO.Path.Combine(this.tempPics,System.IO.Path.GetFileName);
文件。副本(s,destFolder,true);
}
DirectoryInfo Dir2=新的DirectoryInfo(this.tempPics);
FileInfo[]pics=Dir2.GetFiles();
this.picsInTempFolder=(来自pics中的FI
选择(FI.FullName)).ToList();
foreach(此.picsintemp文件夹中的字符串s)
{
this.picsToDisplay.Add(this.LoadImage));
}
this.indexCounter=0;
this.imgBox.Source=(位图图像)this.picsToDisplay[this.indexCounter];
this.tbxPictureName.Text=System.IO.Path.GetFileName(this.picsInTempFolder[this.indexCounter]);
stringList.Clear();
pics=null;
}
}
}
捕获(异常扩展)
{
这个.WriteToRichTextBox(exp.ToString());
}
}

第一次按下clearimages按钮,我得到了关于正在使用的文件的异常。第二次这样做,实际上效果很好


在清除所有列表和删除临时目录中的文件之间放置thread.sleep不起作用。但不知何故,它需要一种延迟。

尝试这种加载图像的方法

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

嗨,我刚刚试过你的代码,但是当我清除并处理所有东西时,我得到了和我在第一篇文章中发布的相同的崩溃;(lmfao发现了问题。我的代码中有一个双击功能。这就是文件一直在使用的原因。签名不同,这就是编译器没有抱怨的原因。
 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;
        }