Sometime we may need to download images from external location to our Windows Phone 7 Media Library. Few days ago, I tried to implement the same in one of my application. Initially, I thought that it is very easy to implement but later noticed the complexity. I did few R&D with Mayur Tendulkar to implement the same and came up with a solution. Thanks to Mayur for helping me out.

 

It this post, I will discuss the way of implementation of it with the full code snippet. Continue reading to check out the downloading mechanism of external images to Media Library.

 

Using WebClient to Download the Image

To start with the implementation, design your page with a button. Register the click of the button and in the implementation part of it, write the following code:

 
private void SaveImageClicked(object sender, GestureEventArgs e)
{
    var webClient = new WebClient();
    webClient.OpenReadCompleted += WebClientOpenReadCompleted;
    webClient.OpenReadAsync(new Uri(IMAGE_URL, UriKind.Absolute));
}

 

We will use WebClient to download the image asynchronously by passing the image URL as a parameter to the OpenReadAsync() method of WebClient object. Those who don’t know how to download specific thing asynchronously in Silverlight or Windows Phone 7, this part will help you.

 

Saving the Image to Media Library

Once you call the OpenReadAsync() method by passing the image URL to the method, the WebClient object will download it as a Stream. Once the download is complete, you need to save the image in the “Saved Images” folder inside the Windows Phone 7 Media Library.

 

To implement this, first create the StreamResourceInfo of the downloaded stream and store a temporary image in the Isolated Storage. Then use WritableBitmap to open the file from IsolatedStorage and save it as JPEG image file in the Media Library.

 

The below code snippet will help you to understand the saving mechanism:

 
void WebClientOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    const string tempJpeg = "TempJPEG";
    var streamResourceInfo = new StreamResourceInfo(e.Result, null);
 
    var userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
    if (userStoreForApplication.FileExists(tempJpeg))
    {
        userStoreForApplication.DeleteFile(tempJpeg);
    }
 
    var isolatedStorageFileStream = userStoreForApplication.CreateFile(tempJpeg);
 
    var bitmapImage = new BitmapImage { CreateOptions = BitmapCreateOptions.None };
    bitmapImage.SetSource(streamResourceInfo.Stream);
 
    var writeableBitmap = new WriteableBitmap(bitmapImage);
    writeableBitmap.SaveJpeg(isolatedStorageFileStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 85);
 
    isolatedStorageFileStream.Close();
    isolatedStorageFileStream = userStoreForApplication.OpenFile(tempJpeg, FileMode.Open, FileAccess.Read);
 
    // Save the image to the camera roll or saved pictures album.
    var mediaLibrary = new MediaLibrary();
 
    // Save the image to the saved pictures album.
    mediaLibrary.SavePicture(string.Format("SavedPicture{0}.jpg", DateTime.Now), isolatedStorageFileStream);
 
    isolatedStorageFileStream.Close();
}

 

Once you save the image, don’t forget to close the associated stream.

 

End Note

I hope, you came to this post by searching online on the image storing mechanism in windows phone 7 media library and this post helped you to understand and implement the functionality. If you still have any doubts and/or have any queries, drop a note at the comments section. I will try to help you as soon as possible.

 

Don’t forget to share your feedback. Follow my blog, Twitter, Facebook and Email Newsletter for regular updates on articles, news and tips.

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.