'Program crashes on startup with ImageBrush
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'.
Solution 1:[1]
The build action of your image may be set to Content. Change it to be a Resource instead.
Solution 2:[2]
You have a backslash in your image source...
Assuming that Images is a folder in your solution your image source should be:
Source="../Images/Koala.jpg"
Or even better:
Source="pack://application:,,,/Images/Koala.jpg"
Solution 3:[3]
Had the same problem. Right click on the image and select properties
change the Build Action to Resource and save your project.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eric Johnson |
| Solution 2 | Dean Kuga |
| Solution 3 |
