79
votes

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?

1
Did you set a reference to the dll?Emond Erno
Build mode of the image? (Seeing stuff in the designer says nothing)H.B.
Thank you @H.B. , like I commented on the answer, I didn't even include the image into project. I guess the designer and runtime behave different is a little bit confusing for fresh men like me.tete
@tete: I would not recommmend the use of the designer at all, i for one do not use it as it easily produces horrible code with Margins and Alignments all over the place. People easily end up using the snapping instead of laying out things dynamically using the different panels as intended.H.B.
@H.B. so you are designing xaml solely by code, or you are using some designing tool like blend? I am actually using VS12 RC which integrates blend. But I am mainly a coder so I'd rather not learn it unless my boss says so :)tete

1 Answers

206
votes

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