【问题标题】:disposing generic list of bitmapimages处理位图图像的通用列表
【发布时间】:2012-01-28 09:25:29
【问题描述】:

如何在 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 'reference not set to an instance of an object'

如果我注释掉那一行,它会在这部分崩溃:File.Delete(path); 提示我要删除的图片正在使用中。

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

我想念什么?

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

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

}

第一次按下 clearimages 按钮时,我得到关于正在使用的文件的异常。第二次做,效果还不错。

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

【问题讨论】:

    标签: c# wpf image dispose bitmapimage


    【解决方案1】:

    试试这个加载图片的方法

     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 发现了问题。我的代码中有一个 double button_click 函数。这就是为什么文件一直在使用的原因。签名不同,这就是编译器没有抱怨的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多