如何在XAML中引用图像资源?

时间:2023-01-24 18:54:40

I am very new to WPF. I put an Image control on a Window and I would like to set the image on the control to an image that is stored in a project resource file named "Resources.resx". The name of the image in the resource file is 'Search'. Could someone show me how I might go about doing this?

我对WPF很感兴趣。我将图像控件放在窗口上,我想将控件上的图像设置为存储在名为“Resources.resx”的项目资源文件中的图像。资源文件中图像的名称是'Search'。有人能告诉我该怎么做吗?

5 个解决方案

#1


94  

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:

如果映像在资源文件夹中,并且其构建操作被设置为Resource。您可以参考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.

当我在资源文件夹下有一个名为RibbonImages的文件夹时。

#2


23  

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:

如果您在项目的图标文件夹中有一个图片,并且它的构建动作是“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;

#3


7  

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:

这里有一个更干净的方法来实现这一点:假设资源。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.

<图像源= {绑定{ x:静态界面:资源。Search} />,其中‘Search’是资源的名称。

#4


3  

  1. Add folders to your project and add images to these through "Existing Item".
  2. 将文件夹添加到项目中,并通过“现有项目”将图像添加到这些项目中。
  3. XAML similar to this: <Image Source="MyRessourceDir\images\addButton.png"/>
  4. 类似的:如何在XAML中引用图像资源?
  5. F6 (Build)
  6. F6(建立)

#5


-2  

already discussed, seems like there's an example for you here

已经讨论过了,这里有个例子

You can also take a look here, where you can have a deeper understanding on what's possible.

你也可以看看这里,在那里你可以对什么是可能的有更深的理解。

#1


94  

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:

如果映像在资源文件夹中,并且其构建操作被设置为Resource。您可以参考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.

当我在资源文件夹下有一个名为RibbonImages的文件夹时。

#2


23  

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:

如果您在项目的图标文件夹中有一个图片,并且它的构建动作是“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;

#3


7  

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:

这里有一个更干净的方法来实现这一点:假设资源。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.

<图像源= {绑定{ x:静态界面:资源。Search} />,其中‘Search’是资源的名称。

#4


3  

  1. Add folders to your project and add images to these through "Existing Item".
  2. 将文件夹添加到项目中,并通过“现有项目”将图像添加到这些项目中。
  3. XAML similar to this: <Image Source="MyRessourceDir\images\addButton.png"/>
  4. 类似的:如何在XAML中引用图像资源?
  5. F6 (Build)
  6. F6(建立)

#5


-2  

already discussed, seems like there's an example for you here

已经讨论过了,这里有个例子

You can also take a look here, where you can have a deeper understanding on what's possible.

你也可以看看这里,在那里你可以对什么是可能的有更深的理解。