tags:

views:

100

answers:

2

Hej

I am trying to load an (embedded) image in a wpf application, using an Uri but I keep getting an exception.

The code is:

new BitmapImage(new Uri("pack://application:,,,,/Icons/m.png"));

(In case it isn't clear, I am trying to load the m.png file from the Icons folder, which has been marked as an embedded ressource).

and the exception is

NotSupportetException (the URI prefix is not recognized)

Can anybody tell me what the uri should have been?

+3  A: 

Three commas must be instead of four in your string:

new BitmapImage(new Uri("pack://application:,,,/LibName;component/Icons/m.png"));

LibName - points to assembly where resource is hosted.

Eugene Cheverda
This throws an exception as well.
Darin Dimitrov
I don't think so. If this is a WPF app then it shouldn't throw an exception.
Eugene Cheverda
You are right, but it was not enough to fix the problem.
tomjen
@Eugene you are right, it shouldn't but I am not calling this from an WPF class.
tomjen
+2  A: 

You may take a look at this blog post. The solution is to register a custom uri parser so that it recognizes the pack protocol:

UriParser.Register(
    new GenericUriParser(GenericUriParserOptions.GenericAuthority), "pack", -1
);
Darin Dimitrov
In case anyone comes to this page looking for the answer, can you let us know what it was in that post that helped?
Foole
@Foole, this fixes the problem: `UriParser.Register(new GenericUriParser(GenericUriParserOptions.GenericAuthority), "pack", -1)`.
Darin Dimitrov