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

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

问题描述

对于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屋!

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