views:

38

answers:

1

I'm loading an image in WPF by using the BitmapImage class. My code works perfectly when I give an absolute path for the UriSource but not when I try and use a relative path.

My XAML is:

<Image Width="50" Name="MemberImage" HorizontalAlignment="Left">
    <Image.Source>
        <BitmapImage DecodePixelWidth="50" UriSource="questionmark.jpg" />
    </Image.Source>
</Image>

The questionmark.jpg is added to the project with a Build Action of Resource and Copy to Output Directory set to Copy always. The error I get is "The file [file name] is not part of the project or its 'Build Action' property is not set to 'Resource'". This works when I use an absolute path for the UriSource but that obviously won't do.

How should I be defining my UriSource in the XAML?

A: 

I cannot reproduce the problem on my computer:

  • I add the jpg by choosing Add existing item in the Project menu
  • I then set its Build Actio to Resource and Copy to Output directory to always.
  • My XAML is the same.

Works perfectly here, even after moving the .exe and renaming the jpg. Something else must be biting you!

Dabblernl
Yes, something is. I have now looked a bit further and the file isn't being copied to the output directory, despite being set to 'Resource' and 'Copy always' as you've done. Any idea why the copy wouldn't be being done?
Val M
If I change the Build Action to Content the file is copied over but the version in the output directory isn't usable because of the same error in the XAML "The file [filename] is not part of project or its 'Build Action' property is not set to 'Resource'." Copying it manually to the output directory, leaving it set to 'Resource' in the solution and rebuilding the project gives the same error.
Val M
When you set the build action to 'resource' the jpg is embedded in the .exe or .dll so is not seperately identifiable. So this is the expected result. It seems that you did not include the file in your project: click on the Project menu and choose 'Add existing item'. That should solve it.
Dabblernl
Dabblernl, it is added to my project - that's how I originally added it - so there's some other reason why it's not available.
Val M
I am sorry Val, I cannot reproduce the error. Start again with a fresh small project and see what happens then. If that works as expected try to add features to this new project that make it more look like the real project. Hopefully you will stumble on what's the problem then. Good luck!
Dabblernl