处理位图图像的通用列表

发布于 2024-12-22 18:41:46 字数 3026 浏览 3 评论 0原文

如何处理 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_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 比删除临时目录中的文件更有效。但不知怎的,它需要某种延迟。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山有枢 2024-12-29 18:41:46

尝试这种加载图像的方法

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

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;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文