如何将Bitmapimage保存到本地文件夹Windows Store应用程序

时间:2012-12-28 23:06:17

标签: windows image c#-4.0 windows-8 windows-store-apps

private async void Button_Click2(object sender, RoutedEventArgs e)
    {
        CameraCaptureUI camera = new CameraCaptureUI();
        camera.PhotoSettings.AllowCropping = true;
        camera.PhotoSettings.CroppedAspectRatio = new Size(16, 9);
        StorageFile photo = await camera.CaptureFileAsync(CameraCaptureUIMode.Photo);

        if (photo != null)
        {
            IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);
            bmp.SetSource(stream);
            imageGrid.Source = bmp;
        }
    }

    private async void saveButton_Click3(object sender, RoutedEventArgs e)
    {


        //Save Image to the file
        //What code goes here?

    }

拍摄照片后,我将图像设置为网格(imageGrid),以便用户可以查看。完成一些任务后,我希望用户能够按一个按钮来保存图片和文本信息。我在将文本保存到文件时没有问题,但我似乎无法找到以这种方式保存图像的任何信息。我不想直接从相机保存它。我需要让用户能够将其保存在命令中。   所以我的问题是,如何将捕获的图像作为XAML图像设置到UI后保存?提前谢谢。

1 个答案:

答案 0 :(得分:4)

正如桑德拉已经建议的那样,你需要坚持CaptureFileAsync的结果。该方法已将捕获的图像保存到文件中,并以StorageFile的形式将其返回给您。只需将该引用存储到私有字段,然后在用户点击Save后复制它:

private StorageFile photo;

private async void Button_Click2(object sender, RoutedEventArgs e)
{
    CameraCaptureUI camera = new CameraCaptureUI();
    camera.PhotoSettings.AllowCropping = true;
    camera.PhotoSettings.CroppedAspectRatio = new Size(16, 9);
    photo = await camera.CaptureFileAsync(CameraCaptureUIMode.Photo);

    if (photo != null)
    {
        IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);
        bmp.SetSource(stream);
        imageGrid.Source = bmp;
    }
}

private async void saveButton_Click3(object sender, RoutedEventArgs e)
{
    if (photo != null)
    {
        await photo.MoveAsync(ApplicationData.Current.LocalFolder, newFilename);
    }
}