0

How to set a picked image as background of a grid dynamically, save it in app local storage and retrieve it each time app is launched?

    BitmapImage BgBitmap = new BitmapImage();
    Image BgImg = new Image();
    private async void bgbtn_Click(object sender, RoutedEventArgs e)
    {
        var fop = new FileOpenPicker();
        fop.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        fop.FileTypeFilter.Add(".jpg");
        fop.FileTypeFilter.Add(".png");
        fop.CommitButtonText = "OK";
        fop.ViewMode = PickerViewMode.Thumbnail;

        StorageFile file = await fop.PickSingleFileAsync();
        IRandomAccessStream stream= await file.OpenAsync(FileAccessMode.ReadWrite);

        await file.CopyAsync(ApplicationData.Current.LocalFolder, "BackgroundImg", NameCollisionOption.ReplaceExisting);

        await BgBitmap.SetSourceAsync(stream);
        BgImg.Source = BgBitmap;
    }

Now, how to set this BgImg as "mainGrid" Grid Background? and it will be nice if i can save the picked file in app storage and set tht file as background.

minkimac
  • 65
  • 8

1 Answers1

0
var imageBrush = new ImageBrush();
imageBrush.ImageSource = BgBitmap;
this.mainGrid.BackGround = imageBrush;

this should work for you

RenDishen
  • 928
  • 1
  • 10
  • 30
  • It Worked. Thank you. But there's one more query. I want to save this image in app local storage and access whenever i want. How to do that? Please, can you help? – minkimac Dec 20 '14 at 17:13
  • @minkimac http://stackoverflow.com/questions/4161359/save-bitmapimage-to-file do you saw this code? Maybe it will help you – RenDishen Dec 20 '14 at 17:36
  • Well, that did'nt help but thanks. I'll figure it out. – minkimac Dec 20 '14 at 17:51