1

我是 C# 新手,我想创建一个可以拍照并将自己保存在本地存储中的应用程序 Metro。我知道,我需要使用隔离存储,但我真的不明白如何将它用于图像。我看到了很多关于字符串的例子,但没有看到图片的例子。

如果有人知道该怎么做?实际上,我拍了一张照片,并要求用户将其记录在他想要的地方。但我想在用户拍照后自动记录。这是我目前的代码:

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

        if (photo != null)
        {
            BitmapImage bmp = new BitmapImage();
            IRandomAccessStream stream = await photo.
                                               OpenAsync(FileAccessMode.Read);
            bmp.SetSource(stream);
            ImageSource.Source = bmp;
            ImageSource.Visibility = Visibility.Visible;

            appSettings[photoKey] = photo.Path;


            FileSavePicker savePicker = new FileSavePicker();
            savePicker.FileTypeChoices.Add
                                  ("jpeg image", new List<string>() { ".jpeg" });

            savePicker.SuggestedFileName = "New picture";

            StorageFile ff = await savePicker.PickSaveFileAsync();

            if (ff != null)
            {
                await photo.MoveAndReplaceAsync(ff);                 
            }
        }
    }
4

1 回答 1

1

您需要做的就是用检索本地文件夹中的 StorageFile 对象来替换文件选择器逻辑,例如:

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

   if (photo != null)
   {
      var targetFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("some_file_name.jpg");
      if (targetFile != null)
      {
         await photo.MoveAndReplaceAsync(targetFile);                 
      }
   }
}
于 2013-02-01T20:18:47.030 回答