user3056737
user3056737

Reputation: 9

upload photo using the FileOpenPicker windows8.1

I'm trying to upload photo to my application using the FileOpenPicker and when I write the following code:

FileOpenPicker open = new FileOpenPicker();
open.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
open.ViewMode = PickerViewMode.Thumbnail;

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

// Open a stream for the selected file
StorageFile file = await open.PickSingleFileAsync();

// ImageSource im = (new Uri (file.Path));
ChildPic.Source = new BitmapImage(new Uri(file.Path));

it did not give me an error but the Image control is left blank.

there is value in the Path: C:\Users\Pictures\New folder(15).jpg

Upvotes: 0

Views: 858

Answers (1)

Farhan Ghumra
Farhan Ghumra

Reputation: 15296

new BitmapImage(Uri) will not work with any path. It supports only URI having protocol http, ms-appx, ms-appdata. You have to use stream.

FileOpenPicker open = new FileOpenPicker();
open.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
open.ViewMode = PickerViewMode.Thumbnail;

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

// Open a stream for the selected file
StorageFile file = await open.PickSingleFileAsync();

// ImageSource im = (new Uri (file.Path));
var bmp = new BitmapImage();
using (var strm = await file.OpenReadAsync())
{
    bmp.SetSource(strm);
    ChildPic.Source = bmp;
}

Upvotes: 2

Related Questions