Reputation: 373
I have the following:
<BitmapImage UriSource="{Binding resource}" />
Where resource is a string in a class that holds a location to an image. This works when resource is an absolute path, but doesn't when the path is relative.
I understand I have to use pack URI. But how do I implement it in this case?
Upvotes: 1
Views: 3903
Reputation: 903
Below are 3 ways you can have images in your project.
a. Add image directly into the project, right-click on image, select properties and set Build Action -> Resource. This will compile image into assembly (exe/dll)
<Image Source="Grapes.png" Stretch="None"></Image>
If you created some folder say "Images" inside your project and add images into that folder then path would be
<Image Source="Images\Grapes.png" Stretch="None"></Image>
b. Add image directly into the project, right-click on image, select properties and set Build Action -> Content and "Copy to output directory" to "Copy Always". This will copy the images file beside to EXE in the folder where EXE is created. (bin\debug or bin\release)
<Image Source="Orange.png" Stretch="None"></Image>
If you created some folder say "Images" inside your project and add images into that folder then path would be
<Image Source="Images\Orange.png" Stretch="None"></Image>
c. Do not add image into the folder. Instead copy image directly into folder where EXE will be created (bin\debug or bin\release). When you run the application, siteoforigin will be equal to location of your executable.
<Image Source="pack://siteoforigin:,,,/Apple.png" Stretch="None"></Image>
Upvotes: 2
Reputation: 10020
Try using pack URI for the string which hold a location to an Image
pack://siteoforigin:,,,/yourimagefilename.jpg
Upvotes: 0