Question

I'm porting my Windows Phone 8 app to Windows 8 and I need to select a picture and set an image's source to it! I must port it today. I used this:

FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");

Windows.Storage.StorageFile file = await openPicker.PickSingleFileAsync();

if (file != null)
{
    BitmapImage img = new BitmapImage() { UriSource = new Uri((file.Path + file.DisplayName + file.FileType), UriKind.RelativeOrAbsolute) };
    image.Source = img;
}
else
{
}

And it selected the image but it haven't changed the source and I also used:

FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");

Windows.Storage.StorageFile file = await openPicker.PickSingleFileAsync();

if (file != null)
{
    BitmapImage img = new BitmapImage() { UriSource = new Uri(file.Path, UriKind.RelativeOrAbsolute) };
    bustin.Source = img;
    dt.Start();
    oyunsure.Start();
}
else
{
    dt.Start();
    oyunsure.Start();
}

But both didn't work.

Was it helpful?

Solution

Here you go:

It's a bit trickier than you'd expect.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace App1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            FileOpenPicker opener = new FileOpenPicker();
            opener.ViewMode = PickerViewMode.Thumbnail;
            opener.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            opener.FileTypeFilter.Add(".jpg");
            opener.FileTypeFilter.Add(".jpeg");
            opener.FileTypeFilter.Add(".png");

            StorageFile file = await opener.PickSingleFileAsync();
            if (file != null)
            {
                // We've now got the file. Do something with it.
                var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
                var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
                await bitmapImage.SetSourceAsync(stream);
                bustin.Source = bitmapImage;
                var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
            }
            else
            {
                //OutputTextBlock.Text = "The operation may have been cancelled.";
            }

        }
    }
}

OTHER TIPS

I have write this code for picking an image and showing it in image control.. It is working.

public async Task<bool> pickPhoto()
    {
        FileOpenPicker openPicker = new FileOpenPicker();
        openPicker.ViewMode = PickerViewMode.List;
        openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
        openPicker.FileTypeFilter.Add("*");

        StorageFile file = await openPicker.PickSingleFileAsync();
        if (file != null)
        {
            var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
            BitmapImage image = new BitmapImage();
            image.SetSource(stream);
            imageChangedProfilePic.Source = image;
            imageChangedProfilePic.Stretch = Stretch.Fill;
            return true;
        }
        else
        {
            //  OutputTextBlock.Text = "Operation cancelled.";
            return false;
        }

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top