Reputation: 1121
I'm developing a small WPF application and I'm trying to change the background of a grid to an image the user chooses (stored somewhere on the computer on a different location from the app). Is it possible without having the images included on the project and marked as a Resource? How?
Upvotes: 0
Views: 402
Reputation: 38094
Assuming you Grid
name is grid
, then xaml would be:
<Grid Name="grid">
...
</Grid>
then to set an image programatically to grid
, you should use the following code snippet:
string imgPath=@"E:\anImage.jpg";
grid.Background= new ImageBrush { ImageSource = new BitmapImage(new Uri(imgPath,
UriKind.RelativeOrAbsolute)) };
Upvotes: 1
Reputation: 21
This works for me : 1) Add image in solution (Add => Existing Item)
2)<Grid>
<Grid.Background>
<ImageBrush ImageSource="/App;component/Chrysanthemum.jpg">
</ImageBrush>
</Grid.Background>
</Grid>
Upvotes: 0
Reputation: 5500
you just need to set the image's source URI to the image location
<Image Source="<<URI of image>>"/>
<ImageBrush ImageSource="<<URI of image>>"/>
or you can do the same via binding to allow it to be customised
<Image Source="{Binding Data}"/>
in this example Data is a byte[] stored in the model, but could be anything that converts to an image source
Upvotes: 0