0

I have a bunch of icons as resources. Managed to change MainWindow icon without issues, but doing the same thing on others gives me this error:

Could not find a part of the path 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\resources\add.ico'

I have this in the top section of XAML file:

Icon="resources/add.ico"

So - for some reason it searches for resources in resources folder when in MainWindow.xaml, but in other window it decides to search in VS folder (for unknown reason). How can i fix this?

2 Answers2

0

I would recommend you to use absolute paths using a WPF Pack-URI. These posts explain it quite well:

https://stackoverflow.com/a/2416464/10724593
https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/aa970069(v=vs.100)?redirectedfrom=MSDN

For you it would be pack://application:,,,/resources/add.ico

The basic syntax for it is pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml as stated on the MSDN Article

VollRahm
  • 397
  • 1
  • 16
  • Link only answers should be considered as comments, not the answers – Pavel Anikhouski May 23 '20 at 16:07
  • 1
    Works flawlessly. It appears that MainWindow accepts simplified path, while other windows demand 'expanded' version. Marking as solved. –  May 23 '20 at 17:05
0

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.

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

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

Then, in your code, use something like:

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

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.

For more information look HERE

amitklein
  • 1,302
  • 6
  • 23