Grid myGrid = new Grid(); ImageBrush myImageBrush = new ImageBrush(); myImageBrush.ImageSource = new BitmapImage(new Uri("path/to/image.png", UriKind.Relative)); myGrid.Background = myImageBrush;
Button myButton = new Button(); ImageBrush myImageBrush = new ImageBrush(); myImageBrush.ImageSource = new BitmapImage(new Uri("path/to/image.png", UriKind.Relative)); myButton.Background = myImageBrush;Both examples create an instance of the ImageBrush class and set its ImageSource property to a BitmapImage object that is used to load the image file. The ImageBrush is then assigned to the Background property of either a Grid or Button. The System.Windows.Media namespace and ImageBrush class are part of the WPF PresentationCore library.