Click here to Skip to main content
Click here to Skip to main content

Tagged as

Browse Photo in Windows Phone 8.1

, 5 Aug 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

     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:

   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:

       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)

Share

About the Author

Anis Derbel
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)
Group type: Organisation

9 members

Follow on   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 5 Aug 2014
Article Copyright 2014 by Anis Derbel
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid