无法显示拍摄的照片
Cannot display captured photo
我正在尝试构建一个相机应用程序。我可以拍照并保存。
但这是一个转折点。如果我将照片保存在 ApplicationData.Current.LocalFolder
中,我可以在 Image
控件中显示它。但是当我将它保存在 KnownFolders.CameraRoll
中时,Image
控件不会加载照片。
照片显示在 phone 的画廊(照片)中,所以并不是文件未创建。我也添加了图片库功能。连个例外都没有!
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;
}
访问文件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;
}
我正在尝试构建一个相机应用程序。我可以拍照并保存。
但这是一个转折点。如果我将照片保存在 ApplicationData.Current.LocalFolder
中,我可以在 Image
控件中显示它。但是当我将它保存在 KnownFolders.CameraRoll
中时,Image
控件不会加载照片。
照片显示在 phone 的画廊(照片)中,所以并不是文件未创建。我也添加了图片库功能。连个例外都没有!
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;
}
访问文件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;
}