How can I set the WPF BitmapImage UriSource property to a relative path?

非 Y 不嫁゛ 提交于 2019-12-04 08:59:23

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

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?

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!