locked
Set Source of Image using StorageFile using FilePicker

    Question

  • I am developing an app in which I have called a filePicker and then the image is set as source of userImage. I am getting image through filepicker but it is not setting as source of userImage. My code is

                           

    private void pickPhoto_Tapped(object sender, TappedRoutedEventArgs e)
            {
                MyPictureImage = true;
                FileOpenPicker photochooser = new FileOpenPicker();
                photochooser.ViewMode = PickerViewMode.Thumbnail;
                photochooser.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
                photochooser.FileTypeFilter.Add(".jpg");
                photochooser.FileTypeFilter.Add(".jpeg");
                photochooser.FileTypeFilter.Add(".png");
                //Launch file open picker and caller app is suspended and may be terminated if required
                photochooser.PickSingleFileAndContinue();            
            }

            public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
            {
                var values = args.ContinuationData;
                if (args.Files.Count > 0)
                {
                    StorageFile sf = args.Files[0];

                    ImageEncodingProperties format = ImageEncodingProperties.CreateJpeg();
                    StorageFile Image = await ApplicationData.Current.LocalFolder.CreateFileAsync(sf.Name, CreationCollisionOption.ReplaceExisting);

                    StorageFile sfa = await ApplicationData.Current.LocalFolder.GetFileAsync(sf.Name);

                    UploadImage(sf);
                }
            }

    try
                    {
                        StorageItemThumbnail thumb = await file.GetThumbnailAsync(ThumbnailMode.PicturesView, 250, ThumbnailOptions.UseCurrentScale);
                        userImage.Source = await ImageFromStorageItemAsync(thumb);
                    }
                    catch (Exception ex)
                    {

                    }

    private async static Task<BitmapImage> ImageFromStorageItemAsync(StorageItemThumbnail item)
            {
                BitmapImage image = new BitmapImage();
                await image.SetSourceAsync(item);
                return image;
            }

    Can anybody Help?

    Tuesday, March 24, 2015 11:31 AM

Answers

  • Your code looks good and I wasn't able to reproduce the issue.

    You need to look in your "App.xaml.cs" page and make sure you have modified the "OnActivated" event to be able to navigate to the ContinueFileOpenPicker method.

    protected override void OnActivated(IActivatedEventArgs args)
            {
                var root = Window.Current.Content as Frame;
                var mainPage = root.Content as MainPage;
                if (mainPage != null && args is FileOpenPickerContinuationEventArgs)
                {
                    mainPage.ContinueFileOpenPicker(args as FileOpenPickerContinuationEventArgs);
                }
            }

    Let me know if this helps.


    Abdulwahab Suleiman

    Tuesday, March 24, 2015 10:42 PM
    Moderator