104

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?

Adi
  • 5,089
  • 6
  • 33
  • 47
tete
  • 4,859
  • 11
  • 50
  • 81
  • Did you set a reference to the dll? – Emond Aug 14 '12 at 08:52
  • Build mode of the image? (Seeing stuff in the designer says *nothing*) – H.B. Aug 14 '12 at 08: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. – tete Aug 14 '12 at 09: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. – H.B. Aug 14 '12 at 09:17
  • @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 Aug 14 '12 at 09:24
  • @tete: Yes, i usually just write all XAML by hand. If i were to design an application theme (which in itself would be rather questionable) i might use blend but for simple layouting and certain styling tasks that works just fine. – H.B. Aug 14 '12 at 09:30
  • 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! – tete Aug 14 '12 at 09:32

2 Answers2

263

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.

livan3li
  • 119
  • 2
  • 8
Buzz
  • 6,030
  • 4
  • 33
  • 47
  • 1
    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. – tete Aug 14 '12 at 09:11
  • 35
    and yes, don't forget to do clean and rebuild.. as Buzz stated.. it is easy to miss that.. I did :) – invalidusername Jan 13 '14 at 13:53
  • 1
    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! – j riv May 31 '15 at 01:06
  • 15
    Incredible -- **cleaning** and rebuilding was the issue! I spent hours trying to figure this one out, thanks! – Ed Bayiates Mar 08 '16 at 18:05
  • 1
    Echoing what @jriv said, I had to include the file in my project, just having it in the file was not enough – J King Oct 07 '16 at 16:54
  • 1
    When you access an image like this it seems to work OK. But if the Window is in a WPF Control Library and is actually run from a WinForms project it breaks. My solution was to change the path to: The images should automatically be set with a Resource build action as soon as they're added to the project. – user1040323 Oct 26 '18 at 10:05
  • 1
    Oh VS... now I have to clean upon every build to clear the false errors, see my control in designer, and cure XAML of its squiggle disease. – fartwhif Nov 16 '18 at 17:13
  • 1
    I'm here 9 years later this is the only answer for this problem I could find in the entire StackOverflow community, including other languages. Thank you! – Natan Fernandes Feb 10 '21 at 14:56
  • build action? huh? – john k Mar 11 '23 at 16:42
  • For anyone using .Net(in my case 7), the resource assembly must set true in the project properties, otherwise this error will pop up regardless of the setting of the "Resource" on the image. – John K May 26 '23 at 13:44
-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.

DiskJunky
  • 4,750
  • 3
  • 37
  • 66
  • 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". – Adrian Mole Mar 01 '23 at 12:22
  • 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](/help/how-to-answer). – Community Mar 01 '23 at 12:22