Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Browse Photo in Windows Phone 8.1

0.00/5 (No votes)
5 Aug 2014 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here