WPF throws "Cannot locate resource" exception when loading the image

WpfResources

Wpf Problem Overview


I am having a WPF window, with a background image of one file in my local system. So the XAML file looks like this:

 <Window.Background>
    <ImageBrush ImageSource="/MYASSEMBLY;component/Resources/MyPic.png"/>
 </Window.Background>

In the XAML designer it actually shows this background image, so the image does exist in MYASSEMBLY\Resources folder. However, in the InitializeComponent() function call when I launch the application, it throws the exception

> Cannot locate resource 'resources/mypic.png'.

So I am totally lost now. Are the directories different between the design and runtime so doesn't exist in the runtime?

Wpf Solutions


Solution 1 - Wpf

In Solution Explorer set the image file's Build Action property to Resource. Then clean the solution and rebuild it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionteteView Question on Stackoverflow
Solution 1 - WpfBuzzView Answer on Stackoverflow