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

Windows phone 8 将图像保存在windows phone 8的存储文件中,windows-phone-8,Windows Phone 8,我正在为windows应用商店制作一个windows phone 8应用程序,我正在使用PhotoChooser任务让用户上传个人资料图片 在商店版本中,我使用了streams和FileOpenPicker,但我不知道如何在PhotoChooser任务中使用streams 这就是我在windows应用商店中所做的,它非常完美: StorageFile image; public bunForm() { image = null; this.I

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

在商店版本中,我使用了streams和FileOpenPicker,但我不知道如何在PhotoChooser任务中使用streams

这就是我在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");
    }  
public BunForm()
{
初始化组件();
image=null;
this.photoChooserTask=新的photoChooserTask();
this.photoChooserTask.Completed+=新事件处理程序(photoChooserTask_Completed);
}
存储文件图像;
private void choosePic(对象发送方,路由目标)
{
photoChooserTask.Show();
}
专用异步void photoChooserTask_已完成(对象发送方,PhotoResult e)
{
//这是唯一一行给我的错误
var file=wait openPicker.PickSingleFileAsync();
///
如果(文件!=null)
{
图像=文件;
var stream=await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
如果(文件!=null)
{
//确保在加载映像后处理流
使用(irandomaccesstream fileStream=await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
Show(例如ChosenPhoto.Length.ToString());
//代码以在名为myImage的图像控件中显示页面上的照片。
System.Windows.Media.Imaging.BitmapImage bmp=新系统.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
myImage.Source=bmp;
}
}
}
Debug.WriteLine(“pic done”);
}  
我想知道如何将图像保存到windows phone 8的存储文件中?


您不能使用FileOpenPicker类。
这个问题已经有了答案

因为-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;
}
//首先在某处调用任务
PhotoChooserTask=新的PhotoChooserTask();
task.Completed+=task_Completed;
task.Show();
//处理结果
异步无效任务\u已完成(对象发送方,PhotoResult e)
{
//未选择照片
if(e.ChosenPhoto==null)返回;
//获取文件流和文件名
流photoStream=e.ChosenPhoto;
字符串文件名=Path.GetFileName(例如OriginalFileName);
//将数据持久化到隔离存储中
StorageFile file=wait ApplicationData.Current.LocalFolder.CreateFileAsync(文件名,CreationCollisionOption.ReplaceExisting);
使用(Stream current=await file.OpenStreamForWriteAsync())
{
等待photoStream.CopyToAsync(当前);
}
...
//以后如何读取数据
StorageFile file2=等待ApplicationData.Current.LocalFolder.GetFileAsync(文件名);
Stream imageStream=等待文件2.OpenStreamForReadAsync();
//将文件显示为图像
BitmapImage bi=新的BitmapImage();
bi.SetSource(imageStream);
//将位图指定给XAML中的图像:
img.Source=bi;
}

那么,如果我使用Photochooser任务,有什么可以替代该代码呢?我想将photochooser任务结果保存在StorageFile变量“file”中。如果要保存文件,请尝试使用OpenStreamForWriteAsync,它是在WindowsRuntimeStorage Extensions的WP8上定义的。只需在应用程序中添加以下内容:“使用System.IO
// 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;
}