简体   繁体   中英

XAML Image source

I'm writing a WPF app with VS2015. I have a User Control that contains an Image control. I have my image on the file system in an Images subfolder. In the Design mode, the image showed up fine, but when I ran it, it didn't. I specified the image location thus:

Source="Images/ball.png"

within the Image tag. I tried all the obvious things, like copying the folder to the bin\\Debug folder and such. Experimenting, this error message popped up over the Source tag:

"Could not find a part of the path 'C:\\Program Files (x86)\\New folder\\Microsoft Visual Studio 14.0\\Common7\\IDE\\Images\\red-ball.png'"

I finally got it to show up by giving it the FULL PATH to the image, but I really don't think I should have to. Isn't there a way to give it a relative path that's relative to the application and not the location of the IDE?

First, make sure that your Images subfolder is relative to your project ie:

  • Project
    • Images
      • ball.png

Then with "ball.png" added to your project, set its Build Action to "Resource". Your link will now work as you expect.

I'm guessing its looking for the image in the place you mentioned because it couldn't find it in the resources where it wants to look. Note that sounds and video clips do not work this way, you have to copy those to your build output or use a pack URI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM