Click here to Skip to main content
13,056,397 members (126,212 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

9.4K views
Posted 19 Mar 2012

How to Use the PhotoChooserTask to Launch the Media Library?

, 19 Apr 2012
Rate this:
Please Sign up or sign in to vote.
How to use the PhotoChooserTask to launch the Media Library?

Sometime in our application, we want to launch the Media Library of the Windows Phone 7 device and also want to provide the user with 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 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 tip.

Know About the API

PhotoChooserTask is a sealed class present in the Microsoft.Phone.Tasks namespace which allows the 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; }
    [SecuritySafeCritical]
    public override void Show();
}
  • PixelHeight

    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.

  • PixelWidth

    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.

  • ShowCamera

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

  • Show()

    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 a handle of the chosen photo.

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

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

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));
            break;
    }
}

In the PhotoResult, you can get the original file name with the 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.

Reference: http://www.kunal-chowdhury.com

You may like to follow me on twitter @kunal2383 or may like the Facebook page of my blog http://www.facebook.com/blog.kunal2383.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kunal Chowdhury «IN»
Software Developer (Senior)
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik Developer Expert, Nokia Developer Champion, Windows 10 Champion, Microsoft Rockstar, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows Store (WinRT) app development. He posts his findings, articles, tutorials in his technical blog and CodeProject.

Technical Blog: http://www.kunal-chowdhury.com
Facebook: http://facebook.com/blog.kunal
Twitter : http://twitter.com/kunal2383

You may also be interested in...

Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170728.5 | Last Updated 19 Apr 2012
Article Copyright 2012 by Kunal Chowdhury «IN»
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid