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 .

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

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

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

    3 Comments

  1. romel
    May 28, 2012
    Reply

    getting error in “image1.source”.

  2. romel
    May 28, 2012
    Reply

    aah got it, i forgot to add Image controler on my app , now its solved 🙂

  3. May 28, 2012
    Reply

    Nice to hear that 🙂

Leave A Reply

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

You May Also Like

This blog post will guide you through several effective methods to troubleshoot and resolve the issue of Microsoft Edge not...
Windows 11 offers a range of audio enhancements that can enrich your listening experience. These enhancements include features like virtual...
Windows 11 brings a fresh and visually stunning design to your desktop, and one of the standout features is the...