1

I'm using Visual Studio 2019 Community

How I added the .png to my project.

  1. Open Resources.resx in the project

  2. Click Add Resource -> Add Existing File

  3. Select the .png file to add

It is added to the program. I can see it in resources. It exists in my designer and displays properly in it.

There are other images used in this program that have the exact same properties and that I can call at any time and they work just fine. However, these new ones do not. They are both .png files. I have tried cleaning and rebuilding the solution multiple times as well.

Code for my xaml:

<Image Source="Resources/FileName.png" Width="50" HorizontalAlignment="Left" Margin="5,5,0,0"/>

Resources Properties: Build Action = Embedded Resource

Copy to Output Directory = Do not copy

Image Properties Persistence = Linked at compile time (same for working and not working resources)

Kampai
  • 22,848
  • 21
  • 95
  • 95
  • Does this answer your question? [How to refer to Embedded Resources from XAML?](https://stackoverflow.com/questions/9419611/how-to-refer-to-embedded-resources-from-xaml) – Mark Feldman Feb 11 '20 at 22:21
  • Is this post helping? https://stackoverflow.com/questions/5552618/how-to-reference-image-resources-in-xaml – Louis Go Feb 12 '20 at 01:12

3 Answers3

2

You should mark the Build Action as Resource if you want to access it via folder, otherwise(in your case Embedded Resource) you would have to access the application pack with a more complicated URI.

Robert
  • 2,407
  • 1
  • 24
  • 35
  • Why then does this work for other images that are also Embedded Resources? The application already works as is, its the new items added that do not work. – Timberwolf501st Feb 12 '20 at 15:37
  • Also, I tried changing it to Resource and nothing changed. – Timberwolf501st Feb 12 '20 at 16:53
  • Can you give us examples of how it was previously referenced in XAML. Also, for Resource to work you have to start the URI with a slash -> Source="/Resources/FileName.png" – Robert Feb 13 '20 at 08:17
0

1) Change build action in the image properties to Resource.

2) If your image is located in the Resources folder of your project you can refer to it like below:

<Image Source="/Resources/FileName.png" Width="50" HorizontalAlignment="Left" Margin="5,5,0,0"/>

More detailed information is located in the Microsoft Docs here: Pack URIs in WPF

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
0

Set Build action to Resource,then use the following code

<Image Source="Pack://Application:,,,/Resources/FileName.png"/>