Using image in xaml from Resources.resx

Viewed 772

In my program, I have a resources file named "test" with image "test.png". I want to use it in a XAML file. But when program runs I don't see image. My code:

xmlns:r="clr-namespace:ProjectB.Properties"
...
<Image Source="{Binding Source={x:Static r:Resources.test}}" />

Build Action of the "test.png" image is set to Resource

EDIT:

I wanted to use an image in a resources file to be able to use different images in different globalizations (same as strings in resources file). I don't know it possible in the way I tried but I ended with adding images to normal folder in my project like answers below suggest, to resources file I added paths to image and XAML file looks like this:

<Image
Source="{Binding Source={x:Static r:Resources.test},
                 Converter={StaticResource StrToImgConv}}" />
2 Answers

You don't need to use Resources.resx just add the test.png image to your project e.g in a 'Resources' folder and set the build action to Embedded Resource then you can use it in xaml code as shown below.

<Image Source="pack://application:,,,/Resources/test.png" />

if you used another folder structure in the Project don't forget to change it in the Source of the Image, for example, you want to keep images in 'Resources\images' so you should use the source as below.

<Image Source="pack://application:,,,/Resources/Images/test.png" />

A resources.resx file is completely unnecessary, as that is a file type used for WinForms, not WPF.

  1. Delete your resources file (or just don't reference it as your source)
  2. Keep your test.png file and keep its Build Action as Resource
  3. Edit your xaml to this:
xmlns:r="clr-namespace:ProjectB.Properties"
...
<Image Source="Resources/test.png" />
Related