存储WPF图像资源 [英] Storing WPF Image Resources
问题描述
对于WPF应用程序,出于说明目的,它们将需要10-20个小图标和图像,将它们作为嵌入式资源存储在程序集中是否正确?
如果是这样,我如何在XAML中指定图像控件应从嵌入式资源加载图像?
如果要在多个地方使用图像,则仅将图像数据加载一次到内存中,然后在所有Image
元素之间共享它是值得的./p>
<BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" />
然后,在您的代码中,使用类似以下内容的
: <Image Source="{StaticResource MyImageSource}" />
就我而言,我发现必须将Image.png
文件设置为具有Resource
的构建动作,而不仅仅是Content
.这将导致图像在已编译的程序集中携带.
For a WPF application which will need 10 - 20 small icons and images for illustrative purposes, does storing these in the assembly as embedded resources the right way to go?
If so, how do I specify in XAML that an Image control should load the image from an embedded resource?
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.
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}" />
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屋!