Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a background of WPF Window?

Tags:

background

wpf

I have a simple WPF window. I intend to set it's background to be one of the images I added to project as Embedded Resource. This is what I tried:

<Window x:Class="A_Boggle.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="A-Boggle" Height="300" Width="625" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Name="Game"> <Window.Background>     <ImageBrush ImageSource="background.jpg"></ImageBrush> </Window.Background> <Grid> </Grid> 

But with this, i always get this: "Error 1 The file splash.jpg is not part of the project or its 'Build Action' property is not set to 'Resource'."

Any ideas?

like image 767
sokolovic Avatar asked Oct 07 '10 20:10

sokolovic


People also ask

How do I add a background image in XAML?

In this article, you will learn how to develop background image in Univesal Windows Platform Application, using XAML and C sharp program. Requirements - Visual Studio 2015 Update 3. Step 1 - Open Visual Studio 2015 Update 3 and open the new project. The shortcut key, which can be used is Ctrl+Shift+N.


2 Answers

Go to the image within VS and set the item to be a Resource. Right click -> Properties -> Build Action -> Resource

Update:

You need to change the path if it is in a folder. ie...Resources/background.jpg

like image 105
Aaron McIver Avatar answered Oct 03 '22 22:10

Aaron McIver


you can use this in main.xaml.cs

  InitializeComponent();         ImageBrush myBrush = new ImageBrush();         myBrush.ImageSource =             new BitmapImage(new Uri("F://13.png", UriKind.Absolute));         this.Background = myBrush; 
like image 25
Ahmed Wafi Avatar answered Oct 03 '22 23:10

Ahmed Wafi