I'm trying to create a very simple window with an ImageBrush background in Visual Studio 2013 as a test for a more complex project. The image shows up in the designer, but the program crashes when I start it up. Here's the XAML:
<Window x:Class="BackgroundTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Name="grid1">
<Grid.Background>
<ImageBrush ImageSource="/Images\Koala.jpg" Stretch="Fill"/>
</Grid.Background>
</Grid>
</Window>
And the basically empty C# (like I said, just a test):
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace BackgroundTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
I'm positive it has something to do with the ImageBrush, if I delete that part of the XAML the program will run a blank window as expected. Can anyone help me get this running?
Here's the debug output: 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 '7' and line position '14'.
Got it! I added the image to the program's resources by right-clicking the solution name in the solution explorer -> Properties -> Resources -> Add Resource -> Add Existing file and selecting the picture of the Koala. I changed the code in the XAML to:
ImageSource="Resources/Koala.jpg"
Taking Eric's advice I clicked on Resources in the Solution explorer, and sure enough the picture was there and I was able to change the build action to Resource. After that it worked perfectly, thanks Eric! I'll check your answer as soon as I have enough reputation. I'm fairly new to stack overflow.