1
votes

I am using the following function in my app to capture a photo using MediaCapture class and copy it to clipboard:

        async private void UseCamera()
        {
        var _ImageFormat = ImageEncodingProperties.CreatePng();
        var _fileStream = new InMemoryRandomAccessStream();

        try
        {
            await _mediaCapture.InitializeAsync();
        }

        catch (Exception e)
        {
            new Windows.UI.Popups.MessageDialog(e.Message).ShowAsync();
        }

        try
        {
            await _mediaCapture.CapturePhotoToStreamAsync(_ImageFormat, _fileStream);
        }

        catch (Exception e)
        {
            new Windows.UI.Popups.MessageDialog(e.Message).ShowAsync();
        }

        var _streamRef = Windows.Storage.Streams.RandomAccessStreamReference.CreateFromStream(_fileStream);
        _dataPackage.SetBitmap(_streamRef);


        Clipboard.SetContent(_dataPackage);
        Clipboard.Flush();

    }

The app is running fine on my local machine. But when I try to run it on simulator, i am given this error:

"An exception of type 'System.UnauthorizedAccessException' occurred in SensorGridCamera.exe but was not handled in user code

Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"

Can anyone help me with it?

2
Have you tried running your application as Admin?Middas
try to remove MessageDialog in code temporary and see the result because some time it throws UnauthorizedAccessException. Don't use async void in method, use async TaskFarhan Ghumra
Middas- how to run as admin in simulator?Shobhan Taparia
Xyroid - i reduced code to just initialization of mediacapture object and await _mediaCapture.InitializeAsync(); looks like this line has some issues.. will try the task methodShobhan Taparia

2 Answers

1
votes

It sounds like it's a permissions issue, running as Admin should fix it. Add an Application Manifest to your project. Then change <requestedExecutionLevel level="asInvoker" uiAccess="false" /> to <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> This will force your application to request to be run as Admin when it's run.

1
votes

OK, so I found this thing, which worked for me:

The trick was to pass a MediaCaptureInitializationSettings object in the await _mediaCapture.InitializeAsync(); method as an argument and before you do that, set the object's PhotoCaptureSource property to VideoPreview and voila!

            MediaCaptureInitializationSettings _cameraSettings1 = new MediaCaptureInitializationSettings();
            _cameraSettings1.PhotoCaptureSource = PhotoCaptureSource.VideoPreview;
            await _mediaCapture.InitializeAsync(_cameraSettings1);

Hope it helps. Cheers :)