/// <summary> /// Takes a photo to a StorageFile and adds rotation metadata to it /// </summary> /// <returns></returns> private async Task TakePhotoAsync() { var stream = new InMemoryRandomAccessStream(); Debug.WriteLine("Taking photo..."); await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream); try { var file = await _captureFolder.CreateFileAsync("SimplePhoto.jpg", CreationCollisionOption.GenerateUniqueName); Debug.WriteLine("Photo taken! Saving to " + file.Path); var photoOrientation = CameraRotationHelper.ConvertSimpleOrientationToPhotoOrientation(_rotationHelper.GetCameraCaptureOrientation()); await ReencodeAndSavePhotoAsync(stream, file, photoOrientation); PickImage.LoadImagesFromFileClicked(file); Debug.WriteLine("Photo saved!"); } catch (Exception ex) { // File I/O errors are reported as exceptions Debug.WriteLine("Exception when taking a photo: " + ex.ToString()); } }
/// <summary> /// Takes a photo to a StorageFile and adds rotation metadata to it /// </summary> /// <returns></returns> private async Task TakePhotoAsync() { // While taking a photo, keep the video button enabled only if the camera supports simultaneously taking pictures and recording video //VideoButton.IsEnabled = _mediaCapture.MediaCaptureSettings.ConcurrentRecordAndPhotoSupported; // Make the button invisible if it's disabled, so it's obvious it cannot be interacted with //VideoButton.Opacity = VideoButton.IsEnabled ? 1 : 0; var stream = new InMemoryRandomAccessStream(); Debug.WriteLine("Taking photo..."); await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream); try { var file = await _captureFolder.CreateFileAsync("SimplePhoto.jpg", CreationCollisionOption.GenerateUniqueName); Debug.WriteLine("Photo taken! Saving to " + file.Path); var photoOrientation = CameraRotationHelper.ConvertSimpleOrientationToPhotoOrientation(_rotationHelper.GetCameraCaptureOrientation()); await ReencodeAndSavePhotoAsync(stream, file, photoOrientation); Debug.WriteLine("Photo saved!"); } catch (Exception ex) { // File I/O errors are reported as exceptions Debug.WriteLine("Exception when taking a photo: " + ex.ToString()); } // Done taking a photo, so re-enable the button //VideoButton.IsEnabled = true; //VideoButton.Opacity = 1; }
public async void CapturePhoto_Click(object sender, RoutedEventArgs e) { //Getting access to Pictures folder var myPictures = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures); //Createing the file that will be saved StorageFile file = await myPictures.SaveFolder.CreateFileAsync("night-photo.jpg", CreationCollisionOption.GenerateUniqueName); //Capture a photo to the stream using (var captureStream = new InMemoryRandomAccessStream()) { //Common encoding with JPEG format await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), captureStream); using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { //Decode the image from the memory stream var decoder = await BitmapDecoder.CreateAsync(captureStream); //Encode the image to file var encoder = await BitmapEncoder.CreateForTranscodingAsync(fileStream, decoder); //Getting the current orientation of the device var photoOrientation = CameraRotationHelper.ConvertSimpleOrientationToPhotoOrientation(_rotationHelper.GetCameraCaptureOrientation()); //Including metadata about the photo in the image file var properties = new BitmapPropertySet { { "System.Photo.Orientation", new BitmapTypedValue(photoOrientation, PropertyType.UInt16) } }; await encoder.BitmapProperties.SetPropertiesAsync(properties); await encoder.FlushAsync(); } } }