6

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?

Val M
  • 899
  • 6
  • 16
  • 26

3 Answers3

12

I don't think you need to copy the image to output directory once it's in resource.

Correct way to specify is

<BitmapImage 
    x:Key  = "Logo"
 UriSource = "pack://application:,,,/ApplicationNamespace;component/Images/App/image.png"  
/>

Just replace

ApplicationNamespace with your application namespace

and

Images/App/image.png with your image path in the project

Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240
  • Shouldn't it be than `UriSource = "pack://application:,,,/ApplicationNamespace" ` and not `UriSource = "pack://application:,,,/ApplicationNamespace;component/Images/App/image.png" `? – BendEg Mar 02 '16 at 14:34
  • 2
    In my tests, `ApplicationNamespace` should be replaced by the name of the assembly (as found in project properties), and not by the default namespace of the project. – Evariste Jan 16 '21 at 17:19
2

Image files with the following options in properties
Build Action=Content
Copy to Output Directory=Copy if newer

<BitmapImage x:Key="QuestionMark" UriSource="pack://siteoforigin:,,,/questionmark.png"/>


Reference:
Xaml - Bitmap UriSource - absolute path works, relative path does not, why?

Antonio N
  • 36
  • 5
0

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
  • 15,831
  • 18
  • 96
  • 148
  • 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 Jul 06 '10 at 09:55
  • 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 Jul 06 '10 at 10:10
  • 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 Jul 06 '10 at 12:59
  • 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 Jul 06 '10 at 14:41
  • 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 Jul 06 '10 at 16:12