Windows 10移动相机

时间:2016-09-01 18:54:01

标签: c# exception uwp windows-10-mobile

我试图在Windows 10移动应用程序中使用相机,但是当我拍摄照片并尝试在屏幕上显示时出现错误。

以下是代码:

CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.CroppedSizeInPixels = new Size(200, 200);

StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);

if (photo == null)
{
    // User cancelled photo capture
    return;
}

StorageFolder destinationFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("ProfilePhotoFolder", CreationCollisionOption.OpenIfExists);

await photo.CopyAsync(destinationFolder, "ProfilePhoto.jpg", NameCollisionOption.ReplaceExisting);
await photo.DeleteAsync();

IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();

SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap,
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Premultiplied);

SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);

imageControl.Source = bitmapSource;

异常消息:

  

类型' System.IO.FileNotFoundException'的例外情况发生在   System.Private.CoreLib.dll但未在用户代码中处理

     

附加信息:系统找不到指定的文件。   (HRESULT异常:0x80070002)

     

如果存在此异常的处理程序,则可以安全地继续该程序。"

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:1)

这是因为您删除了photo,但之后尝试读取刚刚删除的photo,因此异常“FileNotFound”将抛出。请删除以下代码行,它将起作用。

 await photo.DeleteAsync();

但我认为您真正要做的是删除从CameraCaptureUI获取的照片,然后从已经复制的本地文件夹中读取照片。在这种情况下,代码应如下:

await photo.CopyAsync(destinationFolder, "ProfilePhoto.jpg", NameCollisionOption.ReplaceExisting);
await photo.DeleteAsync();
StorageFile newphoto = await destinationFolder.GetFileAsync("ProfilePhoto.jpg");
IRandomAccessStream stream = await newphoto.OpenAsync(FileAccessMode.Read);