Part of the problem is that WPF has no context with which to resolve that URL. It's a relative URL, and typically, it would be resolved relative to the base URI of the XAML content in which it's used. If I use exactly the same URL you start with in this code:
public MainWindow()
{
InitializeComponent();
var img = new Image();
Content = img;
var bmp = new BitmapImage();
bmp.BeginInit();
bmp.UriSource = new Uri(@"/ImageResTestLib;component/MyData/SomeStuff/Resources/Img.png", UriKind.RelativeOrAbsolute);
bmp.EndInit();
img.Source = bmp;
img.Width = bmp.PixelWidth;
}
then it works. That's in the codebehind for MainWindow
, obviously.
With one tiny change, moving this line:
Content = img;
to the end, then I get the same DirectoryNotFoundException
as you.
WPF tries to resolve that URI to an actual resource at the point at which you assign the BitmapImage
as the Source
property of that Image
. My first example works because the Image
is in the visual tree, and so it picks up the base URI of MainWindow.xaml
, and resolves that resource URI relative to that base URI.
If you really need to create the Image
before it gets associated with a visual tree, you've got various options. You could actually set the base URI on the image:
img.SetValue(BaseUriHelper.BaseUriProperty, baseUri);
However, that's kind of weird. It's easier just to construct an absolute URI, e.g.:
bmp.UriSource = new Uri(
baseUri,
@"/ImageResTestLib;component/MyData/SomeStuff/Resources/Img.png");
Both of these of course presume that you know what the base URI is. You can find that out by asking in your MainWindow constructor:
public MainWindow()
{
InitializeComponent();
var baseUri = BaseUriHelper.GetBaseUri(this);
...
In your case, that'll be: pack://application:,,,/ImageResTest;component/mainwindow.xaml
This in turn makes it clear what the resolved URI should be: pack://application:,,,/ImageResTestLib;component/MyData/SomeStuff/Resources/Img.png
Interestingly, you say you try that and get an error. Well I'm trying that exact URI, and I'm not getting an error. Just to be clear, here's my modified version of your MyClass
constructor:
public MyClass(Uri baseUri)
{
img = new Image();
var bmp = new BitmapImage();
bmp.BeginInit();
bmp.UriSource = new Uri(baseUri, @"/ImageResTestLib;component/MyData/SomeStuff/Resources/Img.png");
bmp.EndInit();
img.Source = bmp;
img.Width = bmp.PixelWidth;
}
and here's my MainWindow
constructor:
public MainWindow()
{
InitializeComponent();
var obj = new MyData.SomeStuff.MyClass(BaseUriHelper.GetBaseUri(this));
this.Content = obj.Img;
}
This works for me, having followed your instructions. If I understand you correctly, you're seeing a FileNotFoundException
when you do this. This makes me wonder if your instructions have omitted something. E.g., I'd expect to see this error if ImageResTestLib
was strongly named. (If you want to refer to a resource in a strongly-named library, you need a fully qualified assembly display name before the ;component
part.)
Another option would be to use Application.GetResourceStream
, along with the BitmapImage.StreamSource
property. But again, this is going to need a working URL, so you're likely going to hit the same problem as you had before. Once you work out what's different in your project that's stopping pack://application:,,,/ImageResTestLib;component/MyData/SomeStuff/Resources/Img.png
from working, then the basic approach you already have should be fine.