Reputation: 11963
I am trying to initialize a BitmapImage instance in code with wpf style uri.
BitmapImage icon = new BitmapImage(new Uri("pack://application:,,,/MyAssembly;component/Icons/someIcon.ico", UriKind.Absolute));
But the problem is new Uri
throws System.UriFormatException
Invalid URI: Invalid port specified.
what have I done wrong?
Upvotes: 3
Views: 1359
Reputation: 652
We can use the following code
UriParser.Register(new GenericUriParser(GenericUriParserOptions.GenericAuthority), "pack", -1);
before trying to create the uri.
Upvotes: 1
Reputation: 26213
The issue here is that you're attempting to create the Uri
in an application that isn't a normal WPF application. Uri
has a number of built in 'schemes' that are registered with the UriParser
.
The UriParser class enables you to create parsers for new URI schemes. You can write these parsers in their entirety, or the parsers can be derived from well-known schemes (HTTP, FTP, and other schemes based on network protocols).
WPF adds a parser for the 'pack' scheme when a System.Windows.Application
is created, which is the normal entry point for a WPF application. In your case you can just add a call to this in your composition root:
new System.Windows.Application();
Upvotes: 6