如何处置的BitmapSource [英] How to dispose bitmapsource

查看:962
本文介绍了如何处置的BitmapSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的BitmapSource类从我的临时文件夹读取图像,然后使用BitmapMetadata类读取的元数据。

Am using BitmapSource class for reading an image from my temp folder and then reading the metadata using BitmapMetadata class.

BitmapSource img = BitmapFrame.Create(new Uri(filepath));
 BitmapMetadata meta = (BitmapMetadata)img.Metadata;
DateTime datetaken = DateTime.Parse(meta.DateTaken);
System.IO.File.Delete(filepath);

当我试图删除的图像正在一个异常说:该进程无法访问文件文件路径/文件名',因为它正被另一个进程使用。我想删除的图像前处理中的BitmapSource的。虽然我正在寻找解决方案,我得到的信息一样
你不必处置()一个的BitmapSource。与其他一些的框架形象的类,它不换任何原生资源。

While i was trying to delete the image am getting an exception saying "The process cannot access the file 'filepath/filename' because it is being used by another process.".I thought of disposing the bitmapsource before deleting the image. While i was searching for the solution i got info like "You do not have to Dispose() a BitmapSource. Unlike some other "image" classes in the Framework, it does not wrap any native resources.

就让它走出去的范围,垃圾收集器会释放它的内存。在下面的链接的有道处置废弃的BitmapSource 。我只是想删除存在于物理文件夹中的文件。是否有物理路径删除任何适当的方式。
先谢谢了。

Just let it go out of scope, and the garbage collector will free its memory." in the following link Proper way to dispose a BitmapSource .I just want to delete the file that exists in the physical folder. Is there any proper way for the deletion of physical path. Thanks in advance.

推荐答案

您可以做的顶级建议答案<一个href=\"http://stackoverflow.com/questions/542217/load-a-bitmapsource-and-save-using-the-same-name-in-wpf-ioexception\">here并首先将文件复制到一个流和初始化从流例如位图源。

You could do as the top suggested answer here and copy the file to a stream first and initialize the bitmap source from the stream e.g.

        MemoryStream memoryStream = new MemoryStream();

        byte[] fileBytes = File.ReadAllBytes(filepath);
        memoryStream.Write(fileBytes, 0, fileBytes.Length);
        memoryStream.Position = 0;

        BitmapSource img = BitmapFrame.Create(memoryStream);
        BitmapMetadata meta = (BitmapMetadata)img.Metadata;
        DateTime datetaken = DateTime.Parse(meta.DateTaken);
        System.IO.File.Delete(filepath);

我试过这样做,它为我的作品

I've tried this and it works for me

这篇关于如何处置的BitmapSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆