Click here to Skip to main content
15,891,698 members
Articles / Mobile Apps / Windows Mobile
Tip/Trick

Browse Photo in Windows Phone 8.1

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
5 Aug 2014CPOL 10.6K   3  
This tip will show the new way to browse photos with Windows Phone 8.1

Introduction

While developing apps, we need to browse Picture to modify them or to add user's information. Here is the easy way to do it on Windows Phone 8.1.

Background

It is so easy to Browse Photo in Windows Phone 8.0, however Windows Phone 8.1 has included some big changes.

Using the Code

First of all, you must add the capability PictureLibrary in the manifest file.

Add the Action Click on Button to Browse Picture:

C#
private void PickImage_Click(object sender, RoutedEventArgs e)
   {
       try
       {
           var openPicker = new FileOpenPicker
             {
                 SuggestedStartLocation = PickerLocationId.PicturesLibrary,
                 ViewMode = PickerViewMode.Thumbnail
             };

           // Filter to include a sample subset of file types.
           openPicker.FileTypeFilter.Clear();
           openPicker.FileTypeFilter.Add(".bmp");
           openPicker.FileTypeFilter.Add(".png");
           openPicker.FileTypeFilter.Add(".jpeg");
           openPicker.FileTypeFilter.Add(".jpg");

           PickImage(openPicker);
       }
       catch (Exception)
       {

       }
   }

We add the majority of extensions that we use (bmp, png, jpeg, jpg).

Then this code calls the function PickImage that transfers the Picker to file.

Add the following code:

C#
private async void PickImage(FileOpenPicker openPicker)
     {
         try
         {
             this.completionSource = new TaskCompletionSource<StorageFile>();

             openPicker.PickSingleFileAndContinue();

             StorageFile file = await this.completionSource.Task;

             if (file != null)
             {
                img.Source = await MakeImage(file);
             }
     }

After that, we set the Source of the Image, but after the Call of the Function MakeImage that transfers File to BitmapImage.

Add the function MakeImage:

C#
TaskCompletionSource<StorageFile> completionSource;

WriteableBitmap writimage;

async Task<ImageSource> MakeImage(StorageFile file)
 {
     BitmapImage bitmapImage = null;

     if (file != null)
     {
         bitmapImage = new BitmapImage();

         using (var stream = await file.OpenReadAsync())
         {
             await bitmapImage.SetSourceAsync(stream);
         }
     }
     return (bitmapImage);
 }

History

Browsing pictures is one of the famous keys to developing and building a useful app.

License

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


Written By
Software Developer (Junior) Microsoft Student Partners
Tunisia Tunisia
I study Software Engineering , 23 years old , I'm motivated with all Technologies of Microsoft.
Since I have been in the Community of Microsoft as Microsoft Student Partners, I developped many apps on the platform Windows and Phone. Now , it's time to share what I learn here and I'am ready to help Everyone.
You can contact me at any time (anisderbel@outlook.com)
This is a Organisation

9 members

Comments and Discussions

 
-- There are no messages in this forum --