locked
How to add grid background Image in c#

    Question

  • <Grid x:Name="gridimage">
                <Image x:Name="Image" stretch="Fill"/>
            </Grid>
    
    private void Button1_click(object sender, RoutedEventArgs e)
        {
         // NOT WORKING FOR ME
             gridimage.Source = new BitmapImage (new Uri("location"));
             gridimage.Background = ?
         }
    

    I am trying to change images source or image background in c# when a button is click with xaml design UI.


    Saturday, July 12, 2014 6:26 AM

Answers

  • Dear Ashman Malik; What do you want? change background of grid or change

    source of Image?

    Try to Avoid Grid Background Image. If you want to layer two pictures,

    then add another Image tag. and try to change source of image

    using .cs:

    image.Source = new BitmapImage(new Uri("ms-appx:/Assets/pic.jpg"));

    Note: Be sure you added "ms-appx:/" in Uri arguments.



    Saturday, July 12, 2014 8:05 AM

All replies

  • Dear Ashman Malik; What do you want? change background of grid or change

    source of Image?

    Try to Avoid Grid Background Image. If you want to layer two pictures,

    then add another Image tag. and try to change source of image

    using .cs:

    image.Source = new BitmapImage(new Uri("ms-appx:/Assets/pic.jpg"));

    Note: Be sure you added "ms-appx:/" in Uri arguments.



    Saturday, July 12, 2014 8:05 AM
  • Hi,

    Image control do not have Background property. If you want to change the Grid Control background, you can refer to the codes below:

    grid.Background=new SolidColorBrush(Windows.UI.Colors.Blue);

    Or you can use ImageBrush to define the Background:

    <Grid>
            <Grid.Background>
                <ImageBrush ImageSource="/Assets/wall.jpg"/>
            </Grid.Background>
    </Grid>

    Or by codes:

    ImageBrush brush = new ImageBrush();
                BitmapImage bitmapImage = new BitmapImage(new Uri("ms-appx:///Assets/Logo.scale-100.png"));
                brush.ImageSource = bitmapImage;
                grid.Background = brush;

    And If you want to change image control source you can refer to the link below:

    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.ui.xaml.controls.image.source.aspx

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Monday, July 14, 2014 6:19 AM
  •          gridimage.Source = new BitmapImage (new Uri("ms-appx:///Assets/Logo.scale-100.png"));
    Thursday, August 28, 2014 2:49 PM
  • For Grid Background Changing You have to use a property of Change the Image Source


    // BackgroundImage is the Object of Image
    BackgroundImage.Source = new BitmapImage(new Uri("ms-appx:/Assets/Image.png"));

    Hope This Helps,

    Thanks

    Anestia Siven


    Friday, August 29, 2014 11:08 AM