I struggled with this problem for quite some time, and I think that part of what went wrong in the original was the missing word “component”. For example, I had
myBitmapImage.UriSource = new Uri(@"pack://application:,,,/MyApp;images/mona2.jpg");
but had to
... = new Uri(@"pack://application:,,,/MyApp;component/images/mona2.jpg");
The word "component" is not part of the path, despite its appearance - it is a string literal that should be there. What for? Someone thought it would be a good idea, I think.
And for those struggling with the other part of the thing, what about "MyApp"? This is the name of the Assembly. Right-click the name of your project, select "Properties ..." and on the "Application" tab you will see the "Assembly Name:" field.
If you don’t feel like looking for this (or worrying that this might change by breaking your code), you can do this:
String appUri = @"pack://application:,,,/" + System.Reflection.Assembly.GetEntryAssembly().GetName().Name + ";"; String path = appUri + "component/images/mona2.jpg"; myBitmapImage.UriSource = new Uri(path);
The code is not very beautiful, I admit, it can be clearly shortened, but I hope it takes you where you need it. Remember to set the "Build" property in the image file to "Resource"!
John
source share