ImageBrush throws exception when trying to set Window background

user1683456 picture user1683456 · Jul 12, 2013 · Viewed 7.4k times · Source

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

Answer

John Smith picture John Smith · Feb 12, 2014

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.