如何在Windows Phone中设置和保存背景图像?

时间:2014-04-11 13:17:08

标签: 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>(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;
        }
    }

但这不会保存下一次申请午餐的背景图片。 现在,即使重新启动应用程序后,如何将所选照片保存到隔离存储中仍保留为应用程序背景?

3 个答案:

答案 0 :(得分:1)

异步保存图像,仅适用于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;
}

你可以在网上找到一堆资源,只需谷歌吧。 http://msdn.microsoft.com/en-us/library/xf96a1wz(v=vs.110).aspx

答案 1 :(得分:0)

选择时

IsolatedStorageSettings.ApplicationSettings["backgroundImage"]=e.OriginalFileName;

在应用加载

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

答案 2 :(得分:0)

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

只需将您的Bitmap发送到带有名称的序列化程序,然后让它处理剩下的

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

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

EZ_Iso.dll Download and Documentation