38

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.

2
  • 1
    Thanks for your summary of the problem. I have the same problem and will try to workaround it like you suggest.
    – Oskar
    Commented Nov 4, 2009 at 7:37
  • possible duplicate of Loading BitmapImage in code Commented May 25, 2012 at 13:44

1 Answer 1

27

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.

10
  • 3
    @mackenir: Did you ever consider that the URL you posted in your edited question hadn't yet appeared at the time I wrote my response. Rudeness isn't going to get you a good answer.
    – Noldorin
    Commented May 13, 2009 at 13:02
  • 1
    @mackenir: No worries, in that case. Have you tried simply using a relative URI, aa shown in Table 4 on the MSDN page? You might have more luck with that...
    – Noldorin
    Commented May 13, 2009 at 13:30
  • I'll try relative URIs but I think they may be interpreted as file paths. It appears a string that's an absolute file path is interpreted as such, so Im thinking a plain string with no uri scheme is considered a file path.
    – mackenir
    Commented May 13, 2009 at 13:34
  • @mackenir: Last time I wrote an WPF app (actually, it was Silverilght, but it all works the same), I had no trouble dynamically loading resources (images, indeed) using relative URIs, and I was doing it across different assemblies too. If you're not having any luck, I'll see if I can dig that code out.
    – Noldorin
    Commented May 13, 2009 at 13:53
  • It seems that when I specify a Uri in the relative format, the BitmapImage constructor interprets it as a file path. eg new Uri("/Image.png", UriKind.Relative) is interpreted as c:\Image.png. Maybe the BitmapImage constructor doesnt properly deal with pack URIs.
    – mackenir
    Commented May 13, 2009 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.