I come from a mostly web and a little bit Windows Forms background. For a new project, we will be using WPF. The WPF application will need 10 - 20 small icons and images for illustrative purposes. I am thinking about storing these in the assembly as embedded resources. Is that the right way to go?
How do I specify in XAML that an Image control should load the image from an embedded resource?
This worked for me:
If you will use the image in multiple places, then it's worth loading the image data only once into memory and then sharing it between all
Image
elements.To do this, create a
BitmapSource
as a resource somewhere:Then, in your code, use something like:
In my case, I found that I had to set the
Image.png
file to have a build action ofResource
rather than justContent
. This causes the image to be carried within your compiled assembly.I found to be the best practice of using images, videos, etc. is:
Example
Benefits:
Yes, it's the right way. You can use images in the Resource file using a path: