将图像保存在Windows Phone 8的存储文件中

时间:2013-11-10 06:28:36

标签: windows-phone-8

我正在为Windows商店制作应用程序的Windows Phone 8应用程序,我正在使用PhotoChooser任务让用户上传个人资料照片。

在商店版本中我使用了Stream和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的存储文件中?

2 个答案:

答案 0 :(得分:2)

作为noted on MSDN pages - 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;
}

答案 1 :(得分:0)

根据this

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


您不能使用FileOpenPicker类 已经有问题OpenFilePicker not working

的答案