How to set background image using ImageBrush in C# WPF

Back ground in WPF application can be set using XAML at design time or at run time. Using XAML we do it as follows

        <ImageBrush ImageSource="images\abstract-art-artistic-background-1103970 (1).jpg"/>

At Runtime

Using some code you can change back ground of WPF /C# application using ImageBrush and BitmapImage object as follows

  • Create Uri ,which defines the path of the image
  • Using Uri define a bitmap image object which can source for ImageBrush
  • Create a BitmapImage object and set the ImageBrush source
  • Set Background property with ImageBrush

All the above can be done with inline code as follows

 ImageBrush imageBrush = new ImageBrush(new BitmapImage(new Uri("D:\images\abstract-art-artistic-background-1103970 (1).jpg",UriKind.RelativeOrAbsolute)));
                this.Background = imageBrush;

Published by


A developer,teacher and a blogger obsessed with Python,Dart and open source world from India

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.