0

我正在为我为 windows 商店制作的应用程序制作 windows phone 8 应用程序,并且我正在使用 PhotoChooser 任务让用户上传个人资料图片。

在商店版本中,我使用了流和 FileOpenPicker,但我不知道如何将流与 PhotoChooser 任务一起使用。

这就是我在 Windows 商店中的做法,非常完美:

   StorageFile image;

   public bunForm()
    {
        image = null;
        this.InitializeComponent();
    }

   private async void choosePic(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {

        FileOpenPicker openPicker = new FileOpenPicker();
        openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        openPicker.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");

        // Open a stream for the selected file


        var file = await openPicker.PickSingleFileAsync();
        if (file != null)
        {
            image = file;
            var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

            bunPic.Visibility = Visibility.Visible;

            // Ensure a file was selected
            if (file != null)
            {
                // Ensure the stream is disposed once the image is loaded
                using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
                {
                    // Set the image source to the selected bitmap
                    BitmapImage bitmapImage = new BitmapImage();

                    await bitmapImage.SetSourceAsync(fileStream);// bitmapImage.UriSource.ToString();
                    bunPic.Source = bitmapImage;
                }
            }
        }

    }

这是我在 Windows Phone 8 上尝试的方式: 但是 (openPicker.PickSingleFileAsync();) 行给了我错误。

public BunForm()
    {
        InitializeComponent();

        image = null;
        this.photoChooserTask = new PhotoChooserTask();
        this.photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);

    }

    StorageFile image;

private void choosePic(object sender, RoutedEventArgs e)
    {
        photoChooserTask.Show();
    }

private async void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        //this is the only line that gives me error 
        var file = await openPicker.PickSingleFileAsync();
        ///

        if (file != null)
        {
            image = file;
            var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

            if (file != null)
            {
                // Ensure the stream is disposed once the image is loaded
                using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
                {

                    MessageBox.Show(e.ChosenPhoto.Length.ToString());

                    //Code to display the photo on the page in an image control named myImage.
                    System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
                    bmp.SetSource(e.ChosenPhoto);
                    myImage.Source = bmp;
                }
            }

        }
        Debug.WriteLine("pic done");
    }  

我想知道如何将图像保存在 Windows Phone 8 的存储文件中?

4

2 回答 2

2

MSDN 页面所述- OpenFilePicker 不能在 C# WP8 应用程序中使用,但您可以使用PhotoChooserTask轻松上传个人资料图片:

// first invoke the task somewhere
PhotoChooserTask task = new PhotoChooserTask();
task.Completed += task_Completed;
task.Show();        

// handle the result
async void task_Completed(object sender, PhotoResult e)
{
    // no photo selected
    if (e.ChosenPhoto == null) return;

    // get the file stream and file name
    Stream photoStream = e.ChosenPhoto;
    string fileName = Path.GetFileName(e.OriginalFileName);

    // persist data into isolated storage
    StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
    using (Stream current = await file.OpenStreamForWriteAsync())
    {
        await photoStream.CopyToAsync(current);
    }

    ...

    // how to read the data later
    StorageFile file2 = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
    Stream imageStream = await file2.OpenStreamForReadAsync();

    // display the file as image
    BitmapImage bi = new BitmapImage();
    bi.SetSource(imageStream);
    // assign the bitmap to Image in XAML: <Image x:Name="img"/>
    img.Source = bi;
}
于 2013-11-10T11:58:14.950 回答
0

据此_

Windows Phone 8
This API is supported in native apps only.


您不能使用 FileOpenPicker 类。已经有OpenFilePicker not working
问题的答案

于 2013-11-10T07:48:47.890 回答