立即将图像加载到内存中

发布于 2024-08-10 19:26:15 字数 105 浏览 5 评论 0原文

我需要将 WPF 中的 Tiff 图像中的所有帧打开到内存中,然后删除源。之后我最终需要渲染该图像(根据窗口大小调整大小)。我的解决方案非常慢,我无法在第一次请求之前删除文件源。有什么最佳实践吗?

I need to open all frames from Tiff image in WPF into memory and then delete the source. And after that I eventually need to render that image (resized according to window size). My solution is quite slow and I cannot delete file source before the first require. Any best practices?

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

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

发布评论

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

评论(2

陪你搞怪i 2024-08-17 19:26:15

使用 CacheOption = BitmapCacheOption.OnLoad

此选项可以与 BitmapImage.CacheOption 属性一起使用,或作为 BitmapDecoder.Create() 的参数,如果您如果图像加载后想要访问多个帧,则必须使用 BitmapDecoder.Create。在任何一种情况下,文件都将被完全加载并关闭。

另请参阅我对此问题

更新

的 回答以下代码非常适合加载图像的所有帧并删除文件:

var decoder = BitmapDecoder.Create(new Uri(imageFileName), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
List<BitmapFrame> images = decoder.Frames.ToList();
File.Delete(imageFileName);

当然,您还可以在删除文件后访问decoder.Frames。

如果您更喜欢自己打开流,则此变体也适用:

List<BitmapFrame> images;
using(var stream = File.OpenRead(imageFileName))
{
  var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
  images = decoder.Frames.ToList();
}
File.Delete(imageFileName);

无论哪种情况,它都比创建 MemoryStream 更有效,因为 MemoryStream 在内存中保存数据的两个副本一次:解码后的副本和未解码后的副本。

Use CacheOption = BitmapCacheOption.OnLoad

This option can be used with the BitmapImage.CacheOption property or as an argument to BitmapDecoder.Create() If you want to access multiple frames once the images is loaded you'll have to use BitmapDecoder.Create. In either case the file will be loaded fully and closed.

See also my answer to this question

Update

The following code works perfectly for loading in all the frames of an image and deleting the file:

var decoder = BitmapDecoder.Create(new Uri(imageFileName), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
List<BitmapFrame> images = decoder.Frames.ToList();
File.Delete(imageFileName);

You can also access decoder.Frames after the file is deleted, of course.

This variant also works if you prefer to open the stream yourself:

List<BitmapFrame> images;
using(var stream = File.OpenRead(imageFileName))
{
  var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
  images = decoder.Frames.ToList();
}
File.Delete(imageFileName);

In either case it is more efficient than creating a MemoryStream because a MemoryStream keeps two copies of the data in memory at once: The decoded copy and the undecoded copy.

2024-08-17 19:26:15

我想通了。我必须使用MemoryStream:

MemoryStream ms = new MemoryStream(File.ReadAllBytes(image));
TiffBitmapDecoder decoder = new TiffBitmapDecoder(ms, BitmapCreateOptions.None, BitmapCacheOption.None);
List<BitmapFrame> images = new List<BitmapFrame>();
foreach (BitmapFrame frame in decoder.Frames) images.Add(frame);

I figured it out. I have to use MemoryStream:

MemoryStream ms = new MemoryStream(File.ReadAllBytes(image));
TiffBitmapDecoder decoder = new TiffBitmapDecoder(ms, BitmapCreateOptions.None, BitmapCacheOption.None);
List<BitmapFrame> images = new List<BitmapFrame>();
foreach (BitmapFrame frame in decoder.Frames) images.Add(frame);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文