如何配置bitmapsource

时间:2013-08-31 09:41:00

标签: c# asp.net bitmap

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

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

当我试图删除图像时,我得到一个异常,说“进程无法访问文件'filepath / filename',因为它正被另一个进程使用。”。我想在删除图像之前处理bitmapsource。当我在寻找解决方案时,我得到的信息就像 “你不必Dispose()一个BitmapSource。与Framework中的其他”图像“类不同,它不会包装任何本机资源。

让它超出范围,垃圾收集器将释放它的内存。“在以下链接Proper way to dispose a BitmapSource。我只想删除物理文件夹中存在的文件。有没有正确的方法删除物理路径。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

您可以作为最高建议答案here并首先将文件复制到流中,然后从流中初始化位图源,例如

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

我试过这个,它对我有用

相关问题