My build includes an image using BuildAction == Resource. I want to get BitmapImage from this inline resource.
I can load BitmapImage from a file as follows:
var bitmap = new BitmapImage(new Uri(path));
But how can I create a Uri that will reference an image of an embedded resource?
When I try to create a ' package URI (for example, pack://application:,,,/MyImage.png or pack://application:,,,/MyAssembly;component/MyImage.png ), an exception is thrown:
System.UriFormatException "Invalid URI: The port was expected due to a colon (": "), but the port could not be parsed."
I found a fix in UriFormatException in this post
However, when applying this hotfix, I still get exceptions trying to load BitmapImage from the package URI.
When using the format pack://application:,,,/Image.png I get a NullReferenceException exception, and when using the format pack://application:,,,/AssemblyName;component/Image.png I get a NotSupportedException exception. Uri prefix is ββnot recognized.
Summary My problem was that I was trying to use a "batch URI" in the process before any WPF / window / etc control was created, so the "pack" URI was not registered yet (other WPF requirements "stuff "you also donβt need to be installed, because manually registering the package scheme does not in itself fix the problem). The solution was to wait until we instantiated my WPF user control to use the package URI.
resources wpf bitmapimage
mackenir May 13 '09 at 12:41 2009-05-13 12:41
source share