How to download and save images in WP7 Media Library?

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))
    var isolatedStorageFileStream = userStoreForApplication.CreateFile(tempJpeg);
    var bitmapImage = new BitmapImage { CreateOptions = BitmapCreateOptions.None };
    var writeableBitmap = new WriteableBitmap(bitmapImage);
    writeableBitmap.SaveJpeg(isolatedStorageFileStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 85);
    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);


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.

If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter , Facebook , Google+ and LinkedIn . I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.


  1. HI kunal,
    Thanks for your blog about working with images. How can we show the .gif image in WP7. I had tried lot of things but its looking not possible for me. Can you help me to show any .gif image in wp7. I also tried the ImageTool given by codeplex but result is the same.

    Rachit Gaur

  2. You might know that, .gif image support is not there in Silverlight. I guess, there are some workarounds to do so but not sure.

  3. Hi Kunal-chowdhury: I enjoyed reading your article on LightSwitch.
    In File menu there is a 'Save Application definition'.
    Below that there is "Save selected item as..." This is grayed out. How can i make it come to life and what does it do. I am also trying to save my Table configurations. How can i do that? Your expert help is appreciated. thanks.Rao Inala.

  4. Please upload the Sample Working code of the above scenario....

  5. Hi Umar,

    The full code is already shared. You just need to place it in your code and pass the proper IMAGE_URL there. What else you need? :S

  6. it shows an error at bitmapImage.SetSource(streamResourceInfo.Stream);

  7. Hi Kunal, do you know how can I download a set of images synchronously and then save them one by one in the IsolatedStorage?
    Thanks in advance

  8. Hi Alejandro,

    Why do you want to download the images Synchronously? This will make the UI freeze and in such scenario your application may get rejected from Marketplace submission.

  9. Hi Kunal, I am developing an app that shows you facebook friends birthdays.
    I get my friends' list by FQL. I parse the Json facebook returns and save the UserId, name, birthday, and profile pic into a collection.
    Actually the profile pic is a Uri that is shown in an Image control. To avoid the Image controls in the listbox download the profile img each time they are loaded I would want to have them stored lets say in the isolated Storage. To do that I need to go through the URI collection and download one by one. i tried to do this in a ForEach with the Webclient class, but not all the items are download.
    Thanks for the answer!

  10. what if i want do the same work but for Media element(video or music)Help??

  11. Can you brief about the error please?

  12. Hi Kunal,

    Thanks for the resource you are shared

  13. Thank u very much...This article helped me exactly on what I was searching for...

  14. Hi Kunal
    is this code also applicable to windows phone 8 ?

  15. Hi Kunal,

    Can u tell me please after saving image to MediaLibrary how we can fetch image from their by using path of the image.

    1. Hi Amit,

      You can then use the PhotoChooserTask described here: to read images from Media Library.

  16. Hi Kunal,

    I'm able to deserialize images from url via json but I'm stuck on how to display them on a xaml page one image after the other. Kindly help. Thanks

    1. Hi Allan,

      You have to create a collection and bind it to controls like ListBox. Don't forget to change the DataTemplate of the listbox to place the image control.


  17. Hi kunal, I want ask you a problem, I have a link, and I replace URL IMAGE by But it shows an error at mediaLibrary.SavePicture(string.Format("SavedPicture{0}.jpg", DateTime.Now), isolatedStorageFileStream);. Can you help me or you can publish code demo in here. Thank you very much.

  18. Hi kunal, I want ask you a problem, I have a link, and I replace URL IMAGE by But it shows an error at mediaLibrary.SavePicture(string.Format("SavedPicture{0}.jpg", DateTime.Now), isolatedStorageFileStream);. Can you help me or you can publish code demo in here. Thank you very much.


© 2008-2016 - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top