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:,,,/MyAssembly;component/MyImage.png), an exception is thrown:
"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".
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.