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)); } } } }
3 Comments
getting error in “image1.source”.
aah got it, i forgot to add Image controler on my app , now its solved 🙂
Nice to hear that 🙂