DCSIMG
Saving and Loading Captured Image To and From WP7 Isolated Storage - Essential WPF

Saving and Loading Captured Image To and From WP7 Isolated Storage

If you read my previous post about WP7, explaining how to use launchers and choosers, you may wonder how to capture an image using the phone camera, then saving it to the phone’s isolated storage.

While it is easy to capture an image, it’s trickier to save it to the isolated storage.

To capture an image you should do this:

public partial class MainPage : PhoneApplicationPage
{
    private byte[] _imageBytes;

    private void buttonCapture_Click(object sender, RoutedEventArgs e)
    {
        ShowCameraCaptureTask();
    }

    private void ShowCameraCaptureTask()
    {
        var cameraTask = new CameraCaptureTask();
        cameraTask.Completed += cameraTask_Completed;
        cameraTask.Show();
    }

    private void cameraTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            // Get the image temp file from e.OriginalFileName.
            // Get the image temp stream from e.ChosenPhoto.
            // Don't keep either the stream or rely on the temp
            // file name as they may be vanished!
             
            // Store the image bytes.
            _imageBytes = new byte[e.ChosenPhoto.Length];
            e.ChosenPhoto.Read(_imageBytes, 0, _imageBytes.Length);

            // Seek back so we can create an image.
            e.ChosenPhoto.Seek(0, SeekOrigin.Begin);

            // Create an image from the stream.
            var imageSource = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
            image.Source = imageSource;
        }
    }
}

Having the captured image bytes, lets see how to save the image in the Isolated Storage.

private void SaveToLocalStorage(string imageFileName, string imageFolder)
{
    if (_imageBytes == null)
    {
        return;
    }

    var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
    if (!isoFile.DirectoryExists(imageFolder))
    {
        isoFile.CreateDirectory(imageFolder);
    }                       

    string filePath = Path.Combine(imageFolder, imageFileName);
    using (var stream = isoFile.CreateFile(filePath))
    {
        stream.Write(_imageBytes, 0, _imageBytes.Length);
    }
}

Now that you’ve saved the captured image in the iso storage, let’s see how to load it from there:

private void LoadFromLocalStorage(string imageFileName, string imageFolder)
{
    var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
    if (!isoFile.DirectoryExists(imageFolder))
    {
        isoFile.CreateDirectory(imageFolder);
    }

    string filePath = Path.Combine(imageFolder, imageFileName);
    using (var imageStream = isoFile.OpenFile(
        filePath, FileMode.Open, FileAccess.Read))
    {
        var imageSource = PictureDecoder.DecodeJpeg(imageStream);
        image.Source = imageSource;
    }
}

You can download the code from here.

Published Tuesday, October 12, 2010 11:42 AM by Tomer Shamam

Comments

# re: Saving and Loading Captured Image To and From WP7 Isolated Storage

Monday, November 22, 2010 1:05 AM by Matt

Thanks for this, it was a great help to me.

# re: Saving and Loading Captured Image To and From WP7 Isolated Storage

Monday, May 23, 2011 1:43 PM by Punit

How do we save the picture taken so that it appears in the Windows Phone 'Picture' tool instead of Local Storage?

# re: Saving and Loading Captured Image To and From WP7 Isolated Storage

Monday, May 23, 2011 3:18 PM by Tomer Shamam

Hi Punit,

Add reference to Microsoft.Xna.Framework and then:

var library = new MediaLibrary();

library.SavePicture("Image Name", imageStreamOrBytes);

# re: Saving and Loading Captured Image To and From WP7 Isolated Storage

Tuesday, June 21, 2011 5:33 AM by evon

Hi i was usinf the below code to retrieve image that is saved in the isolated storage.

But i would like it to save multiple images without overwritting the image saved previously.

could anyone help to edit my code.

                       try

           {

               using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())

               {

                   //if (isf.FileExists(App.imagePath));

                   //    isf.CreateFile(App.imagePath);

                       //isf.DeleteFile("myImage.jpg");

                   using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(App.imagePath, FileMode.Append, isf))

                   {

                       var bmp = new WriteableBitmap(inkCanvas, inkCanvas.RenderTransform);

                       bmp.SaveJpeg(isfs, bmp.PixelWidth, bmp.PixelHeight, 0, 100);

                   }

               }

               MessageBox.Show("Can");

               MessageBox.Show(App.imagePath);

               NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));

           }

           catch (Exception exc)

           {

               MessageBox.Show(exc.Message);

           }

Thank in advance

# re: Saving and Loading Captured Image To and From WP7 Isolated Storage

Tuesday, June 21, 2011 4:49 PM by Tomer Shamam

Hi Evon,

What exactly are you trying to do? Load an image from ISO store, change it in memory then write the new image without dleting the previous one?

If so, create another file on ISO store, then use the new stream to save the new image.

# re: Saving and Loading Captured Image To and From WP7 Isolated Storage

Monday, October 10, 2011 6:26 AM by Paulo Flores

Hello, This is good. Do you know how can I import pictures from the media library to the isolated storage and from the isolated storage to the media library? thanks.

# re: Saving and Loading Captured Image To and From WP7 Isolated Storage

Tuesday, October 11, 2011 8:54 AM by Tomer Shamam

Hi Paulo,

1. Add reference to Microsoft.Xna.Framework assembly.

2. Use MediaLibrary, to load media.

3. Use IsolatedStorageFile, to save to iso store file and folders.

# re: Saving and Loading Captured Image To and From WP7 Isolated Storage

Wednesday, February 29, 2012 5:24 PM by Sinchana

I wanted to store an image in a particular location(a folder) other than Isolated Storage. Is it possible to create a location on device and store images and retrieve it back so that it is accessible by other Applications also?

# re: Saving and Loading Captured Image To and From WP7 Isolated Storage

Thursday, March 01, 2012 7:45 AM by Sapna

Hi,

Can i save Images to a folder in a particular location which is accessible to all the Application.

# re: Saving and Loading Captured Image To and From WP7 Isolated Storage

Sunday, March 04, 2012 1:20 PM by Tomer Shamam

Hi,

The only shared follder you can share image is in the Pictures HUB. You can also share an image having a cloud or custom service.

To do that, use MediaLibrary.SavePicture located in Microsoft.Xna.Framework.Media namespace. Here is a link:

msdn.microsoft.com/.../microsoft.xna.framework.media.medialibrary_methods(v=xnagamestudio.40).aspx

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:
Powered by Community Server (Commercial Edition), by Telligent Systems