WP7 Saving to Media Library

Update : My buddy Tim Heuer pointed out that when you are saving an image to the library, you need to make sure that you have the correct orientation.   Instead of repeating the information here, I will instead point you to his great blog post on the subject. GO HERE to check it out.

 

In the last post, we showed you how to capture a picture in your application when hooking to the ‘extras’ menu on windows phone.  Once you have manipulated the picture you will want to save it in the media library.  In this post we will walk you through the steps to do that.

 

Once you have made the changes to the picture you captured (Crop, Color, Black and White, whatever), you then want to save your image to IsolatedStorage.

 

To do this we first need to add a using statement to the top of our class file.

 

using System.IO.IsolatedStorage;

 

Remember in our last post we saved the image returned from the camera in a WriteableBitMap.  (see below code above //Save to Global Bitmap)

 

(This is the code from the last post –  We only added the last line)

 

MediaLibrary library = new MediaLibrary();
Picture picture = library.GetPictureFromToken(queryStrings["token"]);

//Create bitmap
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(picture.GetImage());
WriteableBitmap picLibaryImage = new WriteableBitmap(bitmap);
retrievePic.Source = picLibaryImage;

//Save to Global Bitmap

App.CapturedImage = picLibaryImage;

 

We are going to continue this code by first adding it to a Global Variable placed inside App.xaml.cs called Captured Image

//Global variables for the WriteableBitmap objects used throughout the application.
public static WriteableBitmap CapturedImage;

 

Now that we have that saved globally, we want to first save the picture to IsolatedStorage THEN to the media library.  This would normally be done when an event is fired, like a user clicking a save button.

 

First save the image to isolated storage

 

//Create filename for JPEG in isolated storage
String tempJPEG = "TempJPEG.jpg";

//Create virtual store and file stream. Check for duplicate tempJPEG files.
var myStore = IsolatedStorageFile.GetUserStoreForApplication();
if (myStore.FileExists(tempJPEG))
{
myStore.DeleteFile(tempJPEG);
}
IsolatedStorageFileStream myFileStream = myStore.CreateFile(tempJPEG);

//Encode the WriteableBitmap into JPEG stream and place into isolated storage.
Extensions.SaveJpeg(App.CapturedImage, myFileStream, App.CapturedImage.PixelWidth, App.CapturedImage.PixelHeight, 0, 85);
myFileStream.Close();

We then use the file we just saved to create a stream so we can save it to the media library

 

//Create a new file stream.
myFileStream = myStore.OpenFile(tempJPEG, FileMode.Open, FileAccess.Read);

//Add the JPEG file to the photos library on the device.
MediaLibrary library = new MediaLibrary();
Picture pic = library.SavePicture("SavedPicture.jpg", myFileStream);
myFileStream.Close();

And that’s all you need to do.

 

In the next post we will show you how to use the WPConnect.exe tool to Debug your application without using Zune.  This allows you to navigate to the pictures hub while still connected to Visual Studio.

 

Happy Coding – Daniel Egan – DotNetDoc