我正在尝试构建一个相机应用程序。我可以拍照并保存。
但是接下来是扭曲。如果我将照片保存在ApplicationData.Current.LocalFolder
中,我可以在Image
控件中显示该照片。但是当我将其保存在KnownFolders.CameraRoll
时,Image
控件无法加载照片。
照片显示在手机的图库(照片)中,因此并非文件未创建。我也添加了图片库功能。这甚至都不是例外!
async private void Capture_Photo_Click(object sender, RoutedEventArgs e)
{
//Create JPEG image Encoding format for storing image in JPEG type
ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
//create storage file in local app storage
//StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("Photo.jpg",CreationCollisionOption.ReplaceExisting);
//// create storage file in Picture Library
StorageFile file = await KnownFolders.CameraRoll.CreateFileAsync("Photo.jpg", CreationCollisionOption.GenerateUniqueName);
// take photo and store it on file location.
await captureManager.CapturePhotoToStorageFileAsync(imgFormat, file);
// Get photo as a BitmapImage using storage file path.
BitmapImage img = new BitmapImage();
BitmapImage bmpImage = new BitmapImage(new Uri(file.Path));
// show captured image on Image UIElement.
imagePreivew.Source = bmpImage;
}
答案 0 :(得分:1)
访问PicturLibrary文件时有所不同。我们需要通过调用StorageFile.OpenAsync(FileAccessMode)来加载具有所需访问模式的文件流。
将您的代码更改为以下您的工作:
async private void Capture_Photo_Click(object sender, RoutedEventArgs e)
{
//Create JPEG image Encoding format for storing image in JPEG type
ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
//create storage file in local app storage
//StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("Photo.jpg",CreationCollisionOption.ReplaceExisting);
//// create storage file in Picture Library
StorageFile file = await KnownFolders.CameraRoll.CreateFileAsync("Photo.jpg", CreationCollisionOption.GenerateUniqueName);
// take photo and store it on file location.
await captureManager.CapturePhotoToStorageFileAsync(imgFormat, file);
var picstream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
BitmapImage img = new BitmapImage();
img.SetSource(picstream);
imagePreivew.Source = img;
}