public async Task <TakePhotoResponse> TakePhotoAsync(TakePhotoRequest takePhotoRequest) { var cameraWasUsed = false; if (IsTakingTimelapse()) { return(new TakePhotoResponse() { ErrorMessage = $"Timelapse {currentTimelapseId?.ToString()} is being taken", }); } try { var path = EnsureLocalDirectoryExists(); await cameraInUse.WaitAsync(); cameraWasUsed = true; using (var imgCaptureHandler = new ImageStreamCaptureHandler(path, takePhotoRequest.ImageType)) { var stopwatch = Stopwatch.StartNew(); if (takePhotoRequest.QuickMode) { await camera.TakeRawPicture(imgCaptureHandler); } else { await camera.TakePicture(imgCaptureHandler, takePhotoRequest.GetImageEncoding(), takePhotoRequest.GetPixelFormatEncoding()); } stopwatch.Stop(); var localFilePath = imgCaptureHandler.GetFilepath(); var blobName = await UploadFileAsync("photos", localFilePath); var fi = new FileInfo(localFilePath); Logger.Log($"New photo: {fi.Length} bytes, in {stopwatch.ElapsedMilliseconds}ms"); if (takePhotoRequest.DeleteLocalFile) { File.Delete(localFilePath); } return(new TakePhotoResponse() { BlobName = blobName, LocalFilePath = localFilePath, DeleteLocalFile = takePhotoRequest.DeleteLocalFile, PixelFormat = takePhotoRequest.PixelFormat, ImageType = takePhotoRequest.ImageType, QuickMode = takePhotoRequest.QuickMode, }); } } finally { if (cameraWasUsed) { cameraInUse.Release(); } } }