1
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 中后保存?提前致谢。

4

1 回答 1

4

正如 Sandra 已经建议的那样,您需要坚持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);
    }
}
于 2012-12-29T05:29:45.197 回答