wpfresourcesbitmapimagepack-uri

How can I get a BitmapImage from a Resource?


My assembly includes an image with BuildAction==Resource. I want to obtain a BitmapImage from this embedded resource.

I can load a BitmapImage from file like this:

var bitmap = new BitmapImage(new Uri(path));

But how to I create a Uri that will refer to an embedded resource image?

When I try and create a 'pack URI' (for example pack://application:,,,/MyImage.png or pack://application:,,,/MyAssembly;component/MyImage.png), an exception is thrown:

System.UriFormatException "Invalid URI: A port was expected because of there is a colon (':') present but the port could not be parsed."

I found the fix, to the UriFormatException in this blog post

However, with that fix applied, I still get exceptions trying to load a BitmapImage from a pack URI.

When using the pack://application:,,,/Image.png format, I get a NullReferenceException, and when using the pack://application:,,,/AssemblyName;component/Image.png format, I get a NotSupportedException "The Uri prefix is not recognized".


Summary My problem was that I was trying to use a 'pack URI' in a process before any WPF control/window/etc had been instantiated, so the 'pack' URI scheme was not yet registered (other WPF required 'stuff' must also not be set too, because manually registering the pack scheme doesn't itself fix the problem). The solution was to wait until after instantiating my WPF usercontrol to use pack URIs.


Solution

  • This MSDN page has all the information you might want to know about resource URIs in WPF (often called pack URIs). You're going to want to use relative URIs more often probably, so see Table 4, which should be of particular use.

    If you want a briefer overview of resource (pack) URIs, see this blog post. It shows that syntax is indeed relatively simple:

    pack://application:,,,/ResourceFile.xaml
    
    pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml
    

    However, there are a few quirks to work out (in my experience), so often finding the correct resource URI requires a bit of experimentation.