Windows 8 c#将相机的图片保存在本地存储中

时间:2013-02-01 14:52:21

标签: c# windows-8 microsoft-metro local-storage

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

如果有人知道该怎么做?实际上我拍了一张照片然后让用户把它记录在他想要的地方。但是在用户拍照后我想要一个自动记录。这是我的代码:

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);                 
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

您需要做的就是将File Picker逻辑替换为在Local文件夹中检索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);                 
      }
   }
}