3

I added some images as resources to my WPF project, changed their Build Action to Resource in Solution Explorer and added them to my Main Window like this:

<Image ... Source="Resources/user.png"/>

Everything was working until I added and removed one project from my solution, then these images stopped showing both in design and runtime. The <Image Source/> tag in XAML editor and the Error List now show the message:

Could not find a part of the path "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Resources\user.png".

I tried restarting Visual Studio and the computer, and also cleaned and rebuilt it, but I still don't get the images.

Thiago Rosa
  • 311
  • 1
  • 12
  • Without looking at the project, it sounds like the resource was in the project you removed from the solution – Tony Vitabile Dec 08 '15 at 13:37
  • @TonyVitabile I doubled checked, the resource is in the correct project – Thiago Rosa Dec 08 '15 at 13:40
  • 1
    Try to *Restart* your Visual Studio. Or *Clean* and *Rebuild* your solution. – Salah Akbari Dec 08 '15 at 13:42
  • 1
    Are you sure the URI is correct? Did you rebuild the solution after removing the project? – Tony Vitabile Dec 08 '15 at 13:42
  • @user2946329 and TonyVitabile, I tried that as well, I've edited my question to include this info. – Thiago Rosa Dec 08 '15 at 13:47
  • Did you copy & paste the message in your question from visual studio or did you type it in? Because the path ought to read "C:\Program Files (x86)..." -- notice the space between "Files" and "(x86)", which is missing in your question. Also, are you really working on your project in the visual studio executable directory? Not a good idea if you are. The code should be in a project folder well away from either of the Program Files folders. The image should be in a subfolder of that folder. – Tony Vitabile Dec 08 '15 at 14:09
  • 1
    @Tony Vitable I have the same problem, and of course I'm working in my Projects own directory. Was wondering why visual studio has the crazy idea of looking for my Image in its own directory? – Squirrelkiller Aug 29 '16 at 14:16
  • @MarlonRegenhardt: You got me. If you ever figure it out, please let me know! – Tony Vitabile Aug 29 '16 at 15:06

1 Answers1

6

When you use <Image Source="" />, the Source is relative to the XAML file path. The XAML file was on the root, and I moved it to a View folder. I didn't know about this until I read this comment.

The images are stored in a Resources folder inside the project, so in this case the Source property must start with a forward slash to point it to root, like this:

<Image ... Source="/Resources/user.png"/>
Community
  • 1
  • 1
Thiago Rosa
  • 311
  • 1
  • 12
  • 1
    If I do that, visual studio assumes with "root" I mean C:/ and of course doesnt find my Resource folder there. Any ideas? – Squirrelkiller Aug 29 '16 at 14:18