Set background image on grid in WPF using C# Set background image on grid in WPF using C# wpf wpf

Set background image on grid in WPF using C#


All of this can easily be acheived in the xaml by adding the following code in the grid

<Grid>    <Grid.Background>          <ImageBrush ImageSource="/MyProject;component/Images/bg.png"/>         </Grid.Background></Grid>

Left for you to do, is adding a folder to the solution called 'Images' and adding an existing file to your new 'Images' folder, in this case called 'bg.png'


Did you forget the Background Property. The brush should be an ImageBrush whose ImageSource could be set to your image path.

<Grid>    <Grid.Background>        <ImageBrush ImageSource="/path/to/image.png" Stretch="UniformToFill"/>    </Grid.Background>    <...></Grid>


I have my images in a separate class library ("MyClassLibrary") and they are placed in the folder "Images". In the example I used "myImage.jpg" as the background image.

  ImageBrush myBrush = new ImageBrush();  Image image = new Image();  image.Source = new BitmapImage(      new Uri(         "pack://application:,,,/MyClassLibrary;component/Images/myImage.jpg"));  myBrush.ImageSource = image.Source;  Grid grid = new Grid();  grid.Background = myBrush;