简体   繁体   English

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

[英]How to set and save background image in windows phone?

I'm using this code, so user can sets custom background image for the application: 我正在使用此代码,因此用户可以为应用程序设置自定义背景图片:

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

but this won't save background image for next application lunch. 但这不会为下一次应用午餐保存背景图像。 now how can I save chosen photo to isolated storage to remains as app background even after restarting the application? 现在,即使重新启动应用程序后,如何将选择的照片保存到隔离的存储中也可以保留为应用程序背景?

Save image asynchronously, applies to WP8 only. 异步保存图像,仅适用于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);
        }
    }

Read image synchronously both WP7.x WP8: 同步读取图像和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;
}

You can find a bunch of resources online, just google it. 您可以在网上找到大量资源,只需将其谷歌搜索即可。 http://msdn.microsoft.com/en-us/library/xf96a1wz(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/xf96a1wz(v=vs.110).aspx

When Choosing 选择时

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

On app loading 在应用程式载入中

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

You can use the free EZ_Iso.dll to do this. 您可以使用免费的EZ_Iso.dll来执行此操作。

Just send your Bitmap off to the serializer with a name and let it handle the rest 只需使用一个名称将您的位图发送给序列化器,然后让它处理其余的

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

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

EZ_Iso.dll Download and Documentation EZ_Iso.dll下载和文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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