For a WPF application which will need 10 - 20 small icons and images for illustrative purposes, is storing these in the assembly as embedded resources the right way to go?
If so, how do I specify in XAML that an Image control should load the image from an embedded resource?
A resource is an object that can be reused in different places in your application. WPF supports different types of resources. These resources are primarily two types of resources: XAML resources and resource data files. Examples of XAML resources include brushes and styles.
XAML. WPF image control is a versatile control. We will add the image by using the Syntax: Image Source property. Source Property: From the above example, we have seen that the Source Property is used to define the image we want to display.
Topmost - The default is false, but if set to true, your Window will stay on top of other windows unless minimized.
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:
<BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" />
Then, in your code, use something like:
<Image Source="{StaticResource MyImageSource}" />
In my case, I found that I had to set the Image.png
file to have a build action of Resource
rather than just Content
. This causes the image to be carried within your compiled assembly.
I found to be the best practice of using images, videos, etc. is:
<Image Source="/WPFApplication;component/Images/Start.png" />
Benefits:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With