C# 如何在windows phone中设置和保存背景图像?

C# 如何在windows phone中设置和保存背景图像?,c#,windows-phone-7,windows-phone-8,isolatedstorage,C#,Windows Phone 7,Windows Phone 8,Isolatedstorage,我正在使用此代码,因此用户可以为应用程序设置自定义背景图像: private void Button_Click(object sender, RoutedEventArgs e) { PhotoChooserTask photoChooserTask = new PhotoChooserTask(); photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChoose

我正在使用此代码,因此用户可以为应用程序设置自定义背景图像:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        PhotoChooserTask photoChooserTask = new PhotoChooserTask();
        photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
        photoChooserTask.Show();
    }

    void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
            bmp.SetSource(e.ChosenPhoto);

            var imageBrush = new ImageBrush
            {
                ImageSource = bmp,
                Opacity = 0.5d
            };
            App.RootFrame.Background = imageBrush;
        }
    }
private void按钮\u单击(对象发送者,路由目标)
{
PhotoChooserTask PhotoChooserTask=新的PhotoChooserTask();
photoChooserTask.Completed+=新事件处理程序(photoChooserTask_Completed);
photoChooserTask.Show();
}
无效photoChooserTask_已完成(对象发送方,PhotoResult e)
{
if(e.TaskResult==TaskResult.OK)
{
System.Windows.Media.Imaging.BitmapImage bmp=新系统.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
var imageBrush=新的imageBrush
{
ImageSource=bmp,
不透明度=0.5d
};
App.RootFrame.Background=imageBrush;
}
}
但这不会为下一次应用程序午餐保存背景图像。
现在,我如何将所选照片保存到独立存储中,以便在重新启动应用程序后仍保留为应用程序背景?

异步保存图像,仅适用于WP8

public static async Task SaveImageAsync(string imageFileName, BitmapImage image)
{
    // Get Students LocalFolder
    IStorageFolder folder = await ApplicationData.Current.LocalFolder
        .CreateFolderAsync("Images", CreationCollisionOption.OpenIfExists);


        IStorageFile file = await folder.CreateFileAsync(
            imageFileName, CreationCollisionOption.ReplaceExisting);

        using (Stream stream = await file.OpenStreamForWriteAsync())
        {                
            var wrBitmap = new WriteableBitmap(image);
            wrBitmap.SaveJpeg(stream, image.PixelWidth, image.PixelHeight, 100, 100);
        }
    }
同步读取两个WP7.x WP8的图像:

public static BitmapImage LoadImage(string imageFileName)
{
    BitmapImage bitmapImage = null;

    using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication())
    {
         using (var isoStream = isoFile.OpenFile(
             imageFileName, FileMode.Open, FileAccess.Read))
         {
              bitmapImage = new BitmapImage();
              bitmapImage.SetSource(isoStream);
         }
    }

    return bitmapImage;
}
你可以在网上找到很多资源,只需谷歌一下。

选择时

IsolatedStorageSettings.ApplicationSettings["backgroundImage"]=e.OriginalFileName;
正在加载应用程序

image.Source = new BitmapImage(new Uri(IsolatedStorageSettings.ApplicationSettings["backgroundImage"], UriKind.Absolute));

您可以使用免费的EZ_Iso.dll来执行此操作

只需将位图发送到带有名称的序列化程序,然后让它处理其余的部分

//Saving
EZ_Iso.IsolatedStorageAccess.SaveImage(“MyImage”, YourImage);

//Retrieving 
ImageControl.Source = EZ_Iso.IsolatedStroageAccess.GetImage(“MyImage”,Width,Height); 

尝试使用
e.OriginalFileName
并将其存储在隔离存储中