0

I have a couple of images in an Img folder on my project and all those images are set to Build Action: Resource.

I used to refer to all those images with relative URIs, like this:

new Uri("Img/image.png", UriKind.Relative);

I'm not sure whats changed, but it suddenly stopped working and the images are not found. I now have to refer them like this:

new Uri("pack://application:,,,/MyApp;component/Img/image.png");

In order for them to work. I can't figure out why did relative paths suddenly stopped working. (Relative paths are still working in the XAML)

2
  • Can you try to see if this works - new Uri("/Img/image.png", UriKind.Relative);?
    – Rohit Vats
    Aug 9, 2014 at 9:23
  • Also, have you recently changed the assembly for the image resources and moved them to different project? In case yes then you have to use Pack URI's for sure.
    – Rohit Vats
    Aug 9, 2014 at 9:31

1 Answer 1

0

Image files with build action Resource must be referenced by Pack URIs.

From MSDN:

Resource files are configured as MSBuild Resource items and are compiled into assemblies. WPF supports the construction of pack URIs that can be used to identify resource files that are either compiled into the local assembly or compiled into an assembly that is referenced from the local assembly.

WPF automatically adds the prefix pack://application:,,, (or pack://application:,,,/) to image URIs in XAML. When you write

<Image Source="/Img/image.png"/>

WPF automatically converts it into the local assembly pack URI

"pack://application:,,,/Img/image.png".
10
  • Is it possible to set the same behavior for the code behind in WPF, so that relative urls will also be automatically converted into pack urls? Aug 9, 2014 at 8:49
  • @Clemens - new Uri("Img/image.png", UriKind.Relative); works for me even from code behind without any PACK Uri. Copy to Output directory is set to Do not copy and build action is set as Resource.
    – Rohit Vats
    Aug 9, 2014 at 9:14
  • @RohitVats Maybe, but it isnt't meant to work. WPF resource files have to be accessed via Resource File Pack URIs. If it works otherwise, than just by accident. At least you can't rely on such a behaviour.
    – Clemens
    Aug 9, 2014 at 9:17
  • @Clemens - I don't think so it's by accident. Even MSDN sample here show usage without Pack URI's as well.
    – Rohit Vats
    Aug 9, 2014 at 9:21
  • 1
    As per my understanding it's relative to current executing assembly because if I try without pack URI and resources are there within same assembly of usage it works fine but as soon as I moved the usage to separate assembly BOOM, have to restored to Pack URI's. So, yes I buy your point of using Pack URI's. Was just want to know if it's documented anywhere. As you say it's not, so fair enough.
    – Rohit Vats
    Aug 9, 2014 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.