C# UWP Camera Cannot find the file specified

0

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;


    }
c#
uwp
asked on Stack Overflow Apr 10, 2018 by Mike M

1 Answer

0

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

await photo.DeleteAsync(); 

After removing the line it works fine.

answered on Stack Overflow Apr 11, 2018 by Arul Kumar • edited Apr 11, 2018 by Arul Kumar

User contributions licensed under CC BY-SA 3.0