0

I added an image in my Visual Studio project and I am trying to access it in my XAML resource dictionary without specifying the complete path to where it is on my hard drive.

<ImageBrush x:Key="image" ImageSource="Cherries.jpg"></ImageBrush>
mm8
  • 163,881
  • 10
  • 57
  • 88
user8466504
  • 69
  • 2
  • 11

1 Answers1

0

You could use a pack URI.

Make sure that the Build Action of the image is set to its default value of Resource.

You can specify a path like this where you replace "Subfolder" with the name of the folder within your project where the image resource resides:

<ImageBrush x:Key="image" ImageSource="pack://application:,,,/Subfolder/Cherries.jpg" />

If you have put the image in the root folder the path would be:

<ImageBrush x:Key="image" ImageSource="pack://application:,,,/Cherries.jpg" />

If it is located in another assembly, you inlcude the name of this one:

<ImageBrush x:Key="image" ImageSource="pack://application:,,,/ReferencedAssembly;component/Cherries.jpg" />
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Note however that in XAML you can omit the prefix and just write `ImageSource="/Subfolder/Cherries.jpg"` – Clemens May 29 '18 at 15:22