简体   繁体   中英

C# UWP Camera Cannot find the file specified

I'm trying to get this mini app to save the photo taken by the camera but I get this error.System.IO.FileNotFoundException: 'The system cannot find the file specified. (Exception from HRESULT: 0x80070002)'

I followed the Microsoft Tutorial but cant get it to work.

private async void btnTakePhoto_Click(object sender, RoutedEventArgs e)
    {
        CameraCaptureUI captureUI = new CameraCaptureUI();
        captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
        captureUI.PhotoSettings.CroppedSizeInPixels = new Size(200, 200);

        //StorageFile photo  = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/mypic.jpg"));
        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;


    }

From the above mentioned code snippet remove the line that I have written below.

await photo.DeleteAsync(); 

After removing the line it works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM