WinRT,8.1,C#:动态设置图像作为FileOpenPicker选取的背景

时间:2014-12-20 16:51:58

标签: c# image xaml background windows-runtime

如何动态地将拾取的图像设置为网格背景,将其保存在应用本地存储中并在每次启动应用时检索它?

    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;
    }

现在,如何将此BgImg设置为" mainGrid"网格背景? 如果我可以将挑选的文件保存在应用程序存储中并将该文件设置为背景,那将是很好的。

1 个答案:

答案 0 :(得分:0)

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

这应该对你有用