wpfimageuribitmapimagepack-uri

Why is my Image unable to locate the Resource?


I'm stumped. I am trying to display an image embedded in another assembly. I've tried several things, but nothing has worked so far. I'm convinced I'm missing something really simple.

Currently, the Xaml looks something like this:

<Image>
    <Image.Source>
        <BitmapImage UriSource="pack://application:,,,/Name.Of.Assembly;component/Resources/Icons/icon.png" />
    </Image.Source>
</Image>

I originally had Xaml that looked like this:

<Image Source="pack://application:,,,/Name.Of.Assembly;component/Resources/Icons/icon.png" />

I switched it hoping it would make a difference. (It didn't.)

I have a reference to Name.Of.Assembly in the project I am trying to create an image. In the assembly, Name.Of.Assembly, there's a directory called Resources, which has a subfolder called Icons, which contains the image icon.png. The build action on icon.png is set to Embedded Resource.

When I try to run, I get a XamlParseException:

'Initialization of 'System.Windows.Media.Imaging.BitmapImage' threw an exception'

Whose inner exception is:

'Cannot locate resource 'resources/icons/icon.png'

It's like it's not looking in the assembly for the resource. Why does it fail to load the image? What am I missing?


Solution

  • I found a solution to my problem:

    The first issue was probably having my build action set to Embedded Resource instead of Resource. Once I changed this I ran a Build on the project containing the resource, but I noticed nothing different.

    Per Sean Nelson's suggestion, I did a Clean, followed by a Rebuild on the project.

    The image is now displaying.