WPF, ImageSource and Embedded Resources

Well, it took me a couple of hours, but I’ve finally made it! I managed to load an embedded image from a dll using WPF.

In Windows.Forms I would just add a resource to the resource file, and I was able to access the resource easily via Resources.{name_of_resource_here}, but in WPF this doesn’t help me, because I needed an ImageSource.

After googling around for some time I found Tamir Khason’s blog post about his loadResource method, but I was having trouble loading the image since I was trying to loading an image from a referenced assembly (from within the assembly to an image that was supposed to be a resource within the assembly).

I played with different Pack URIs, the first one I found that worked was this:


This is way too ugly, very non-C# IMHO, so I kept on looking until I found a relative path that was a little better:


This isn’t the prettiest piece of code I’ve seen, but it’s a little better (the first one is an absolute uri, where the second is a relative uri).

I later added this method to my WpfUtils class:

public static Image CreateImageFromPath(string path)
Image image = new Image();
image.Source= LoadResource<ImageSource>(path);
image.Width = image.Source.Width;
image.Height = image.Source.Height;
return image;

Now, to load an image I can just use this one-liner:

Image image = CreateImageFromPath(“/{AssemblyName};component/Images/MyImage.png”);

Danno said...

Just a note that you spelled applicateion incorrectly. Copy and paste really burned me :)

Danno said...

And obviously I just spelled it wrong in my comment as well :)

Thanks for the post btw, really saved me a lot of headaches

David Elentok said...

Oops :)
Ok, it's fixed, thanks for pointing it out! (I hope no one else got copy&paste burned...)

Unknown said...
This comment has been removed by the author.
wabash9000 said...

This was useful, but it still took me a very long time to get it to work. It needs to be mentioned that you need to set the 'Build Action' for the images to 'Resource'. This is done by clicking the file in the solution explorer and looking at it's properties. This will allow the image to work with WPF and Forms.

Popular posts from this blog

  • Every once in a while I get this annoying problem where I can't a access my windows shares from other computers on the network, and the only way to get it it to work again is to restart windows. I recently found out that to avoid restarting windows all you need to do is restart the "Server" service: - Win+R => "services.msc" - Right click "Server" and click restart.
  • In all of my days as a programmer a lot of people approached me with the question "Why isn't my code working?" and it's usually because of one of the following reasons: Syntax errors - missing semicolons/parenthesis, bad jQuery/css selectors. Not enough knowledge about the problem domain or the technology. A couple of days ago I was helping a good friend with a coding problem and he told me: "Don't solve it, tell me the strategy to solve it", this was the first time anyone told me that and it got me thinking... he was right, by solving every problem I'm not allowing the young programmers to learn and that gave me the idea to write this post. I tried to think of the ways I deal with coding problems and I came up with several principles. 1. Always write syntactically-clean code I often see people writing code with no coding standard (inconsistent indentation, spacing, etc...), and when I criticize them about it they say "I'm j