简体   繁体   中英

WinRT, 8.1, C#: Dynamically setting an Image as background picked by FileOpenPicker

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.

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

this should work for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM