2

I'm trying to set the background of my WPF window to an image but I'm getting this exception when I try to run it:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll Additional information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '8' and line position '10'.

I don't want to add the image to the project, as I would like to be able to change the image at runtime. My intention is to use databinding to set the background picture during start-up once I have this bit working.

Source Code:

<Window x:Class="ColinsTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Colin's Test Application" 
    WindowStyle="None"
    WindowState="Maximized">
<Window.Background>
    <ImageBrush 
        ImageSource="DeLaRue-Background.jpg"/>
</Window.Background>
<Grid></Grid>
</Window>

Any Ideas? Thanks

4444
  • 3,541
  • 10
  • 32
  • 43
user1683456
  • 91
  • 1
  • 2
  • 7
  • Take a look at the exception's `InnerException`. Most certainly your application is unable to load the image file because it isn't located in the current directory. – Clemens Jul 12 '13 at 15:47

4 Answers4

9

Wrong. It should be set to CONTENT not Resource.

Include in your project (use the file explorer)

Right click on the image > Properties > Advanced.

Build Action: Content

Copy to Output Directory: Copy always.

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
John Smith
  • 91
  • 1
  • 2
2

Your image's build action is probably set to Content, change it to Resource

Set the item (your image file) to be a Resource. (In Visual Studio) Right click,Properties,Build Action,Resource

Mateusz Dembski
  • 427
  • 1
  • 5
  • 15
-1

I had the same problem with

 <Button>
     <Button.Background>
         <ImageBrush ImageSource="/Resources/icon_done.png">
     </Button.Background>
 </Button>

I used / before Resources and it work correctly, but i used solution with properties image ->Properties->Build to Resources. These two actions solved my issue.

dvhh
  • 4,724
  • 27
  • 33
-2

Add the forward "/" to the location

Then the whole ImageSource string should look like

<ImageBrush ImageSource="/Resources/Image1.png" Stretch="None" /> 
David
  • 208,112
  • 36
  • 198
  • 279