在 WPF 中使用资源图像
据我所知,WPF 图像的源是 BitmapSource 对象,而资源图像被读取为位图。我很容易找到一种将图像从 Bitmap 格式转换为 BitmapSource 格式的方法,但我相信每次需要图像时运行此转换在性能和内存消耗方面都是昂贵的(位图本身是静态的,但我猜想转换会在内存中为 BitmapSource 分配一个新对象)。
我考虑过为 BitmapSource 对象保留一个内存中的惰性缓存,这样我只需要为每个图像运行一次转换。然而,我很难相信这是解决这个问题的正确方法。
应用程序的本质是数据随着时间的推移而扩展,并且业务对象永远不会被释放。因此,只有在实现为这些静态图像保留静态资源的情况下,此假设才成立。
我将非常感谢这个问题的正确解决方案。
From what I've seen, WPF images' source is a BitmapSource object while the Resources images are read as bitmap. I've easily found a way to convert images from Bitmap format into BitmapSource format but I believe that running this conversion every time I need an image is just costly in terms of performance and memory consumption (The Bitmaps themselves are static but I guess that the conversion allocates a new object in memory for the BitmapSource).
I thought about holding an in-memory, lazy cache for the BitmapSource objects, that way I will only need to run the conversion once per image. However, I find it hard to believe that this is the proper solution for this issue.
The nature of the application is that the data extends over time and the business objects are never disposed. Therefore, this assumption will only hold in case the implementation will hold static resources for these static images.
I will very much appreciate the proper solution for this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我明白你的意思,但你可以像这样使用资源中的图像。
如果您有一个名为“B1.png”的图像资源,并将其“构建操作”设置为“资源”,那么您可以像这样在 xaml 中使用它。
并在代码后面像这样
I'm not sure I understand what you mean but you can use Image from Resources like this.
If you have an Image resource named "B1.png" and you set "Build Action" to "Resource" for it than you can use it in xaml like this.
and in code behind like this