立即将图像加载到内存中
我需要将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
CacheOption = BitmapCacheOption.OnLoad
此选项可以与
BitmapImage.CacheOption
属性一起使用,或作为BitmapDecoder.Create()
的参数,如果您如果图像加载后想要访问多个帧,则必须使用 BitmapDecoder.Create。在任何一种情况下,文件都将被完全加载并关闭。另请参阅我对此问题
更新
的 回答以下代码非常适合加载图像的所有帧并删除文件:
当然,您还可以在删除文件后访问decoder.Frames。
如果您更喜欢自己打开流,则此变体也适用:
无论哪种情况,它都比创建
MemoryStream
更有效,因为MemoryStream
在内存中保存数据的两个副本一次:解码后的副本和未解码后的副本。Use
CacheOption = BitmapCacheOption.OnLoad
This option can be used with the
BitmapImage.CacheOption
property or as an argument toBitmapDecoder.Create()
If you want to access multiple frames once the images is loaded you'll have to useBitmapDecoder.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:
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:
In either case it is more efficient than creating a
MemoryStream
because aMemoryStream
keeps two copies of the data in memory at once: The decoded copy and the undecoded copy.我想通了。我必须使用MemoryStream:
I figured it out. I have to use MemoryStream: