WPF图像资源 [英] WPF image resources

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

问题描述

我来自一个主要的网络和一点点的Windows窗体背景。对于一个新的项目,我们将使用WPF。 WPF应用程序将需要10 - 20个小图标和图像用于说明目的。我想在组装嵌入资源存储这些。那是正确的方式去?

I come from a mostly web and a little bit Windows Forms background. For a new project, we will be using WPF. The WPF application will need 10 - 20 small icons and images for illustrative purposes. I am thinking about storing these in the assembly as embedded resources. Is that the right way to go ?

我如何在XAML的图像控制应该从嵌入的资源加载图像指定?

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.

要做到这一点,创建一个<一个href="http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource.aspx"><$c$c>BitmapSource作为一种资源的地方:

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

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

然后,在你的code,使用这样的:

Then, in your code, use something like:

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

在我的情况,我发现,我不得不设置 Image.png 文件有资源而不仅仅是内容。这导致图像被编译的程序集内进行。

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天全站免登陆