I sometimes get an error when i try to take a picture within my uwp app. This error is very hard to reproduce and happend a few min ago on desktop platform. I used a hovercam to retrieve the imagebuffer.
public async Task<IBuffer> TakePhotoAsync()
{
Debug.WriteLine("taking picture...");
using (var stream = new InMemoryRandomAccessStream())
{
try
{
await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);
Debug.WriteLine("captured to stream");
}
catch (Exception e)
{
ExceptionHandler.Instance.HandleException(e);
}
//rotate img
return await GetRotatedIBuffer(stream);
}
}
public async Task<IBuffer> GetRotatedIBuffer(IRandomAccessStream stream)
{
//rotate img
using (var imageStream = new InMemoryRandomAccessStream())
{
BitmapDecoder dec = await BitmapDecoder.CreateAsync(stream);
BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(stream, dec);
BitmapRotation rotation = BitmapRotation.None;
switch (App.settings.DefaultCamera.Rotation)
{
case VideoRotation.Clockwise180Degrees:
rotation = BitmapRotation.Clockwise180Degrees;
break;
case VideoRotation.Clockwise270Degrees:
rotation = BitmapRotation.Clockwise270Degrees;
break;
case VideoRotation.Clockwise90Degrees:
rotation = BitmapRotation.Clockwise90Degrees;
break;
}
enc.BitmapTransform.Rotation = rotation;
await enc.FlushAsync();
try
{
await RandomAccessStream.CopyAsync(stream,imageStream);
}
catch (Exception ex)
{
ExceptionHandler.Instance.HandleException(ex);
}
return await StreamHelpers.StreamToIBuffer(imageStream);
}
}
output:
taking picture...
Exception thrown: 'System.Runtime.InteropServices.COMException' in mscorlib.ni.dll
WinRT information: Started
System.Runtime.InteropServices.COMException (0xC00D36B2): The request is not valid in current state
Started
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at KNFB_Reader_Windows10.KNFBCamera.<TakePhotoAsync>d__25.MoveNext()
Exception thrown: 'System.Exception' in mscorlib.ni.dll
Exception thrown: 'System.Exception' in mscorlib.ni.dll
Exception thrown: 'System.Exception' in mscorlib.ni.dll
Exception thrown: 'System.Exception' in mscorlib.ni.dll
The component cannot be found. (Exception from HRESULT: 0x88982F50)
User contributions licensed under CC BY-SA 3.0