WPF throws "Cannot locate resource" exception when loading the image
Asked Answered
G

2

108

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?

Goldwin answered 14/8, 2012 at 8:50 Comment(7)
Did you set a reference to the dll?Railing
Build mode of the image? (Seeing stuff in the designer says nothing)Rollerskate
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.Goldwin
@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.Rollerskate
@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 :)Goldwin
@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.Rollerskate
@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!Goldwin
C
274

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.

Christlike answered 14/8, 2012 at 8:54 Comment(10)
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.Goldwin
and yes, don't forget to do clean and rebuild.. as Buzz stated.. it is easy to miss that.. I did :)Vibratile
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!Gassman
Incredible -- cleaning and rebuilding was the issue! I spent hours trying to figure this one out, thanks!Alice
Echoing what @jriv said, I had to include the file in my project, just having it in the file was not enoughRatel
When you access an image like this <ImageBrush ImageSource="/Resources/bigWaves.png" /> 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: <ImageBrush ImageSource="Resources/bigWaves.png" /> The images should automatically be set with a Resource build action as soon as they're added to the project.Charliecharline
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.Juicy
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!Peculium
build action? huh?Foxworth
For anyone using .Net(in my case 7), the resource assembly must set <UseWPF>true</UseWPF> in the project properties, otherwise this error will pop up regardless of the setting of the "Resource" on the image.Adze
T
-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.

Tripletail answered 23/2, 2023 at 9:39 Comment(2)
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".Overcurious
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.Culdesac

© 2022 - 2024 — McMap. All rights reserved.