How to select a Photo from the Windows Phone Media Library using C# ?

Sometimes , the windows phone developers might be required to select the photo or image from the Windows Phone Media library in their Windows phone App .

A typically example would be uploading the profile picture of the facebook profile from a facebook App etc …

The Windows Phone 7 SDK provides the PhotoChooserTask chooser which allows the users to choose a picture from the album photo gallery or the Media library .

The Chooser “PhotoChooserTask” is defined in the namespace using Microsoft.Phone.Tasks .

To select the photo from the media library , just create an instance of the PhotoChooserTask class and call the Show Method .

The Completed event is called when a picture is selected from the the photo library and you can use one of the parameter PhotoResult to get the selected picture from your photo album.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.IO;
using System.Windows.Media.Imaging;

namespace PhoneApp4
{
   public partial class MainPage : PhoneApplicationPage
   {
     // Constructor
     public MainPage()
     {
         InitializeComponent();
     }
     PhotoChooserTask selectphoto = null;
     private void button1_Click(object sender, RoutedEventArgs e)
     {
         selectphoto = new PhotoChooserTask();
         selectphoto.Completed += new EventHandler<PhotoResult>(selectphoto_Completed);
         selectphoto.Show();

     }

     void selectphoto_Completed(object sender, PhotoResult e)
     {
        if (e.TaskResult == TaskResult.OK)
        {

            BinaryReader reader = new BinaryReader(e.ChosenPhoto);
            image1.Source = new BitmapImage(new Uri(e.OriginalFileName));

        }
     }
   }
}

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
Pin It

6 thoughts on “How to select a Photo from the Windows Phone Media Library using C# ?

  1. Pingback: How to select a Photo from the Windows Phone Media Library using C#

  2. Pingback: Interesting .NET Links - August 16 , 2011 | Tech Blog

  3. Pingback: How to select a Photo from the Windows Phone Media Library using C# ?MobileOSGeek

Leave a Reply

Your email address will not be published. Required fields are marked *