views:

19807

answers:

6
+27  Q: 

WPF image resources

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 ?

+10  A: 

yes it is the right way. You could use the image in the resource file just using the path:

<Image Source="..\Media\Image.png" />

You must set the build action of the image file to "Resource"

ema
Thanks for this. Is there a way to do something similar with an ImageSource, essentially loading the image once into a resource dictionary. I fear that this approach loads the image data multiple times in memory.
Drew Noakes
This will be a mess when you need to refactor your code. You will have to manually change all the image references if your xaml document happens to change namespace. The method described by Drew Noakes is a lot smoother and maintable.
Qua
+1  A: 

If you're using blend, to make it extra easy and not have any trouble getting the correct path for the Source attribute, just drag and drop the image from the Project panel onto the designer.

+36  A: 

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.

Drew Noakes
Very nice solution. It's maintinable and resistant to refactoring unlike the accepted solution.
Qua
@Qua -- looks like the OP accepted my answer on 14 Apr '10. I didn't think SO allowed changing the accepted answer after this long. Perhaps they changed the rules...
Drew Noakes
Would it be possible to do this dynamically? If I have a differing number of images that I would like to load on start-up, could I create a BitmapSource per image and refer to them the same way as above?
Becky Franklin
@Becky - Yes you could, though if you wanted to refer to them in Xaml then you might need to use the `DynamicResource` markup extension instead of `StaticResource`, assuming you would know the keys at compile time. In WPF you can create resource dictionaries at runtime. In fact, that's what happens when you load a Xaml document, it's just that you don't see the equivalent C#.
Drew Noakes
Thanks for the reply :) I'll be refering to them via FindResource, but I think this may save a ridiculous amount of processing in my app now so thanks :)
Becky Franklin
A: 

It worked until I have moved the window and resource png image to another project.

It works if i keep the the resource image in the win.exe project. But I would like to keep the image resource in the same assembly as the xaml file :(.

Any ideas?

WooYek
+9  A: 

I found to be the best practice of using Images,Video,etc is:

  • Change your files "Build action" to "Content"
    • found on the "Right-Click" menu at the Solution Explorer window
  • Image Source in the following format:
    • "/«YourAssemblyName»;component/Images/«YourImage.png»"

Example

<Image Source="/WPFApplication;component/Images/Start.png" />

Benefits:

  • Files are not embedded into the assembly
    • The Resource Manager will raise some Memory Overflow problems with too many Resources (at build time)
  • Can be called between assemblies
Nuno Rodrigues
This same approach works if you embed the resource in the assembly, but you have to set the "Build Action" to "Resource".
Ashley Davis
works perfectly!
Mark
+5  A: 

Full description how to use resources: http://msdn.microsoft.com/en-us/library/aa970494.aspx and how to reference them read "Pack URIs in WPF"

In short, there is even means to reference resources from referenced/referencing assemblies