How to Launch the WP7 Media Library using the PhotoChooserTask?

Sometime in our application, we want to launch the Media Library of the Windows Phone 7 device and also want to provide user the option to view any existing image from that library. So if you want to do this, how will you implement the code?


Windows Phone 7 SDK provides provides API to do this. Using the PhotoChooserTask you can actually launch the photo chooser application and handle the selected image. Continue reading to read more about this process from this small tips.


Know About the API

PhotoChooserTask is a sealed class present in the Microsoft.Phone.Tasks namespace which allows user to launch the Media library and select any existing image from there. The class consists of few properties, one method and one event. Here is the meta data of the class:

public sealed class PhotoChooserTask : ChooserBase<PhotoResult>
    public int PixelHeight { get; set; }
    public int PixelWidth { get; set; }
    public bool ShowCamera { get; set; }
    public override void Show();


Public property
Gets or sets the maximum height and the height component of the aspect ratio for a cropping region set by the user during the photo choosing process.


Public property
Gets or sets the maximum width and the width component of the aspect ratio for a cropping region set by the user during the photo choosing process.


Public property
Gets or sets whether the user is presented with a button for launching the camera during the photo choosing process.


The Show() method actually Shows the Photo Chooser application.


Practical Implementation

If you want to integrate this in your application, create the instance of the PhotoChooserTask and call the Show() method. If you want to handle the user’s selection, register the Completed event which will give you handle of the chosen photo.


Here is the code snippet which will allow user to chose photo from the Media Library:

var photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += PhotoChooserTaskCompleted;


In the completed event implementation, you can get the chosen image as PhotoResult and set the image to your Image control or can use it in other places.


Here is the code snippet of the implementation:

void PhotoChooserTaskCompleted(object sender, PhotoResult e)
    switch (e.TaskResult)
        case TaskResult.OK:
            imageChooser.Source = new BitmapImage(new Uri(e.OriginalFileName));


In the PhotoResult you can get the original file name with complete path and also a stream of the chosen photo. You can use any one of them. Hope this small post was helpful and now you will be able to use this in your Windows Phone 7 application.


Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields.

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.


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