存储 WPF 图像资源 [英] Storing WPF Image Resources

查看:29
本文介绍了存储 WPF 图像资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于需要 10 到 20 个小图标和图像用于说明目的的 WPF 应用程序,将这些作为嵌入资源存储在程序集中是正确的方法吗?

For a WPF application which will need 10 - 20 small icons and images for illustrative purposes, is storing these in the assembly as embedded resources the right way to go?

如果是这样,我如何在 XAML 中指定图像控件应该从嵌入的资源加载图像?

If so, how do I specify in XAML that an Image control should load the image from an embedded resource?

推荐答案

如果您将在多个地方使用图像,那么值得将图像数据仅加载一次到内存中,然后在所有 Image元素.

If you will use the image in multiple places, then it's worth loading the image data only once into memory and then sharing it between all Image elements.

为此,创建一个BitmapSource 作为某处的资源:

To do this, create a BitmapSource as a resource somewhere:

<BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" />

然后,在您的代码中,使用以下内容:

Then, in your code, use something like:

<Image Source="{StaticResource MyImageSource}" />

就我而言,我发现我必须将 Image.png 文件设置为具有 Resource 而不仅仅是 Content 的构建操作>.这会导致在编译后的程序集中携带图像.

In my case, I found that I had to set the Image.png file to have a build action of Resource rather than just Content. This causes the image to be carried within your compiled assembly.

这篇关于存储 WPF 图像资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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