Saving a background image to local storage

In this recipe we will learn how to save image files to a local storage folder. We will make use of the IsolatedStorageFile class.

Getting ready

For this recipe let's copy the simple MyTasks app solution folder we built in the previous recipe and rename the folder to Ch2_Recipe4_MyTasks. After you open the project, just press F5 and run it to make sure you don't get any errors.

How to do it...

Let's add a button control in the settings page to launch the built-in Photo Chooser utility. Once the desired photo is selected we will save the image to local storage.

  1. Open the settings .xaml file and add a text block control and button control inside the ContentPanel grid:
    <TextBlock Height="55" HorizontalAlignment="Left" Margin="12,199,0,0" Name="textBlock1" Text="Bacground Image" TextWrapping="Wrap" VerticalAlignment="Top" Width="115" />
    <Button Content="Browse" Height="77" Margin="119,0,70,331" Name="btnBrowse" VerticalAlignment="Bottom" Click="btnBrowse_Click" />
    
  2. Open the settings.xaml.cs file and add the following two using statements:
    using System.Windows.Media.Imaging;
    using Microsoft.Phone.Tasks;
    
  3. Let's create an instance of PhotoChooserTask, which will let us pick photos using Photo Chooser. Add the following code before the Settings constructor:
    private PhotoChooserTask bkPhotoTask;
    
  4. Add the following code inside the Settings constructor to initialize PhotoChooserTask and then the photo task completed event handler in the Settings constructor:
    // initialize PhotoChooserTask
    bkPhotoTask = new PhotoChooserTask();
    // add an event
    bkPhotoTask.Completed += new EventHandler<PhotoResult>(bkPhotoTask_Completed);
    
  5. In the button event click method, call the PhotoTask.Show method to launch the Photo Chooser. After the photo is selected, the PhotoTask_Completed event is triggered, which calls the method bkPhotoTask_Completed. This method saves the selected image to the BitmapImage stream:
    private void btnBrowse_Click(object sender,RoutedEventArgs e)
    {
    bkPhotoTask.Show();
    }
    private void bkPhotoTask_Completed(object sender, PhotoResult e)
    {
    if (e.TaskResult == TaskResult.OK)
    {
    BitmapImage bkImage = new BitmapImage();
    bkImage.SetSource(e.ChosenPhoto);
    // save the image to local storage
    SaveImageToLocalStorage(bkImage);
    }
    }
    
  6. Add the saved image stream to an isolated storage file. Here the Imaging Extension class is used to encode the image into the JPEG image stream. Then the image is set to app background:
    // save the image to local storage
    private void SaveImageToLocalStorage(BitmapImage bkImage)
    {
    using(IsolatedStorageFile isFile = IsolatedStorageFile.GetUserStoreForApplication())
    {
    WriteableBitmap wImage = new WriteableBitmap(bkImage);
    if(!isFile.DirectoryExists("Images"))
    {
    isFile.CreateDirectory("Images");
    }
    using(IsolatedStorageFileStream imageStream = isFile.OpenFile(@"ImageskgroundImage.jpg", System.IO.FileMode.OpenOrCreate))
    {
    Extensions.SaveJpeg(wImage, imageStream, wImage.PixelWidth, wImage.PixelHeight, 0, 100);
    }
    var app = Application.Current as App;
    if (app == null)
    return;
    var imageBrush = new ImageBrush { ImageSource = bkImage, Opacity = 0.5d };
    app.RootFrame.Background = imageBrush;
    }
    }
    
  7. Press F5 and navigate to the Settings page. Click on the Browse button. This will launch the Photo Chooser:
    How to do it...
  8. In the Photo Chooser, you should be able to see all the albums in your phone. Pick the album:
    How to do it...
  9. Now pick a photo to make it your background. After you select the picture, PhotoChooserTask confirms that Settings Saved Successfully. Click on ok to close the message box.
    How to do it...
  10. As we apply the image application background brush, the Settings page background will display the selected photo. Now you can navigate back to the main page. You should see the selected photo as the background image.
    How to do it...

How it works...

In order to save the image to local store, we use the IsolatedStorageFile class. We first create the Images folder.

Using the IsolatedStorageFileStream we open the image file chosen for the background image. We use the FileMode as OpenOrCreate. If it already exists, then it just opens, otherwise the image is saved to the file stream using the SaveJpeg method of the Extensions class.

There's more...

In this recipe, we explored how to save images and create folders. You can learn more about files and folders at the following MSDN address: http://msdn.microsoft.com/en-us/library/ff626519%28v=VS.92%29.aspx.

See also

Read the Opening and creating a file recipe in this chapter to understand more on Opening and Creating files in local storage. Also, in Chapter 5, Using On-Device Databases, we will learn how to use the on-device databases as another option to save information locally.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset