Reputation: 111
So I recently started learning winforms... but upon asking a question here I learned that WPF applications are much better, thus started learning the latter. I'm making a very basic login screen and ran into a weird glitch. Upon startup the program instantly crashes (Shows a message box saying "Login has stopped working" "A problem caused the program to stop working correctly. Please close the program.")... I found the problem by removing pieces of code (there's not much at the moment, just getting the hang of the basics) - and it's the background image I used.
<Window.Background>
<ImageBrush ImageSource="Background1.jpg"/>
</Window.Background>
This is the piece of code causing the problem, also tried replacing it with grid.background but the exact same error appears. Any ideas as to how to fix this issue are appreciated. (The image is saved in the same folder as the solution, and shows properly in the preview).
Upvotes: 3
Views: 627
Reputation: 660
In order to simply reference an image like that:
- add your image to the project ( right click => Add => new item... ) at the same level as your window
- set its "Build Action" property to "Resource"
- set its "Copy to Output Directory" property to "Do not copy".
Upvotes: 2
Reputation: 220
Try copying the file to either bin\Debug or bin\Release folder depending on the build configuration. The current directory for the program might not be the solution's root directory.
If this works, a better approach would be to add it to the resources and not use any absolute path structure.
Upvotes: 0