Search code examples
c#windowswindows-store-appswindows-store

how to change background image of windows store application in c# code?


I want to change the background image of a windows store app using C#. I'm looking to change the background image similar to the following pseudo code:

This.Background.ImageSource= "dracula-128.png";

Solution

  • One way is to create an ImageBrush, set its ImageSource property, and then assign that to the background. (Code updated based on comment)

    //BitmapImage class is within this namespace
    using Windows.UI.Xaml.Media.Imaging;    
    
    ImageBrush ib = new ImageBrush();
    ib.ImageSource = new BitmapImage(new Uri(@"ms-appx:///dracula-128.png", UriKind.RelativeOrAbsolute));   
    this.Background = ib;