wpf 如何在 XAML 中引用图像资源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5552618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reference image resources in XAML?
提问by Hosea146
I put an Image
control on a Window and I would like to display an image that is stored in a project resource file named "Resources.resx". The name of the image in the resource file is 'Search'.
我Image
在一个窗口上放置了一个控件,我想显示一个存储在名为“Resources.resx”的项目资源文件中的图像。资源文件中图像的名称是“搜索”。
Could someone show me how I might go about doing this?
有人可以告诉我如何去做吗?
回答by Viv
If the image is in your resources folder and its build action is set to Resource. You can reference the image in XAML as follows:
如果图像在您的资源文件夹中并且其构建操作设置为资源。您可以在 XAML 中引用图像,如下所示:
"pack://application:,,,/Resources/Search.png"
Assuming you do not have any folder structure under the Resources folder and it is an application. For example I use:
假设您在 Resources 文件夹下没有任何文件夹结构,并且它是一个应用程序。例如我使用:
ImageSource="pack://application:,,,/Resources/RibbonImages/CloseButton.png"
when I have a folder named RibbonImages under Resources folder.
当我在 Resources 文件夹下有一个名为 RibbonImages 的文件夹时。
回答by RandomEngy
If you've got an image in the Icons folder of your project and its build action is "Resource", you can refer to it like this:
如果您的项目的 Icons 文件夹中有一个图像,并且其构建操作是“Resource”,则可以像这样引用它:
<Image Source="/Icons/play_small.png" />
That's the simplest way to do it. This is the only way I could figure doing it purely from the resource standpoint and no project files:
这是最简单的方法。这是我能想到的唯一方法,纯粹从资源的角度来看,没有项目文件:
var resourceManager = new ResourceManager(typeof (Resources));
var bitmap = resourceManager.GetObject("Search") as System.Drawing.Bitmap;
var memoryStream = new MemoryStream();
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
memoryStream.Position = 0;
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memoryStream;
bitmapImage.EndInit();
this.image1.Source = bitmapImage;
回答by Shrinand
One of the benefit of using the resource file is accessing the resources by names, so the image can change, the image name can change, as long as the resource is kept up to date correct image will show up.
使用资源文件的好处之一是通过名称访问资源,因此图像可以更改,图像名称可以更改,只要资源保持最新,就会显示正确的图像。
Here is a cleaner approach to accomplish this: Assuming Resources.resx is in 'UI.Images' namespace, add the namespace reference in your xaml like this:
这里有一个更简洁的方法来完成这个:假设 Resources.resx 在 'UI.Images' 命名空间中,像这样在你的 xaml 中添加命名空间引用:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:UI="clr-namespace:UI.Images"
Set your Image source like this:
像这样设置你的图像源:
<Image Source={Binding {x:Static UI:Resources.Search}} />
where 'Search' is name of the resource.
<Image Source={Binding {x:Static UI:Resources.Search}} />
其中“搜索”是资源的名称。
回答by FearlessCoward
- Add folders to your project and add images to these through "Existing Item".
- XAML similar to this:
<Image Source="MyRessourceDir\images\addButton.png"/>
- F6 (Build)
- 将文件夹添加到您的项目并通过“现有项目”向其中添加图像。
- XAML 类似于:
<Image Source="MyRessourceDir\images\addButton.png"/>
- F6(建造)