113

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?

7
  • Did you set a reference to the dll? Commented Aug 14, 2012 at 8:52
  • Build mode of the image? (Seeing stuff in the designer says nothing) Commented Aug 14, 2012 at 8:53
  • 1
    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. Commented Aug 14, 2012 at 9:12
  • @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. Commented Aug 14, 2012 at 9:17
  • 1
    @H.B. Than's true. I guess for application like ours, I can also use code at most of the time. The designer is just for reference. I'll avoid moving and placing stuff directly there. Probably our experienced programmers who were working with Windows Form and Access may find it more friendly. Thank you so much! Commented Aug 14, 2012 at 9:32

2 Answers 2

289

In Solution Explorer set the image file's Build Action property to Resource. Then right click the on Solution Explorer panel and do Clean Solution and rebuild it.

Sign up to request clarification or add additional context in comments.

11 Comments

Thank you, that solves the problem. I thought it would load the picture in the runtime so I didn't even include the image into the project.
and yes, don't forget to do clean and rebuild.. as Buzz stated.. it is easy to miss that.. I did :)
hehe, most of us, including the op, didn't really have that property issue, but we didn't expect that it's required to have the image in the solution explorer if it's already in the physical directory. It's pretty cool to be honest that it's required in the explorer. Thanks!
Incredible -- cleaning and rebuilding was the issue! I spent hours trying to figure this one out, thanks!
Echoing what @jriv said, I had to include the file in my project, just having it in the file was not enough
|
-1

I was also facing this problem. You are getting a run time error while running because your images are not automatically added to the solution. Please check that the image exists in Soluion Explorer. If it's not present, add it to your solution.

2 Comments

You should edit your answer to make it easier to read: (1) Don't Use Capital Letters For The Start Of Each Word; (2) Add some punctuation; (3) No need for things like "thank you".
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.