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

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

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;

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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.