如何保存图片库中使用相机拍摄的照片

时间:2013-08-28 10:08:27

标签: windows-8 windows-runtime microsoft-metro windows-store-apps winrt-xaml

我使用下面的代码捕获照片但不确定如何将其保存在图片库或图片库中的文件夹中。非常感谢你对此的帮助。

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;

  StorageFolder storageFolder = KnownFolders.PicturesLibrary;

??? not sure :
     var imagefile = await KnownFolders.PicturesLibrary.CreateFileAsync("MyPhoto.jpeg", CreationCollisionOption.ReplaceExisting);

   photo.CopyAsync(storageFolder,imagefile);

  }

1 个答案:

答案 0 :(得分:2)

你走了。您必须在清单中设置相机和图片库访问功能。

CameraCaptureUI camera = new CameraCaptureUI();
camera.PhotoSettings.CroppedAspectRatio = new Size(16, 9);
StorageFile photo = await camera.CaptureFileAsync(CameraCaptureUIMode.Photo);

//By default the photo will be stored at location
//%localappdata%\Packages\APP_PACKAGE_ID\TempState

if (photo != null)
{
    //await photo.MoveAsync(KnownFolders.PicturesLibrary);
    //OR
    await photo.MoveAsync(KnownFolders.PicturesLibrary, "DesiredPhotoName" + photo.FileType, NameCollisionOption.GenerateUniqueName);
}