Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private async Task<StorageFile> CaptureImage()
- {
- var stream = new InMemoryRandomAccessStream();
- await Viewfinder.CapturePhotoToStreamAsync(stream, ImageEncodingProperties.CreateJpeg());
- var decoder = await BitmapDecoder.CreateAsync(stream);
- var scale = (double)1136 / (double)decoder.PixelWidth;
- var scaledHeight = (uint)(decoder.PixelWidth * 9 / 16);
- var startPointY = (uint)((decoder.PixelHeight - scaledHeight) / 2);
- var bounds = new BitmapBounds();
- bounds.X = 0;
- bounds.Y = (uint)Math.Floor((double)(startPointY * scale));
- bounds.Height = 640;
- bounds.Width = 1136;
- var photoStorage = await ApplicationData.Current.LocalFolder.CreateFileAsync("image.jpg", CreationCollisionOption.ReplaceExisting);
- var outputStream = await photoStorage.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
- outputStream.Size = 0;
- var encoder = await BitmapEncoder.CreateForTranscodingAsync(outputStream, decoder);
- encoder.BitmapTransform.Bounds = bounds;
- encoder.BitmapTransform.ScaledHeight = (uint)Math.Floor((double)(decoder.PixelHeight * scale));
- encoder.BitmapTransform.ScaledWidth = (uint)Math.Floor((double)(decoder.PixelWidth * scale));
- var rotation = App.ViewModel.VideoLocation == Windows.Devices.Enumeration.Panel.Back ? PhotoOrientation.Rotate270 : PhotoOrientation.Rotate90;
- var properties = new BitmapPropertySet();
- properties.Add("System.Photo.Orientation", new BitmapTypedValue(rotation, PropertyType.UInt16));
- await encoder.BitmapProperties.SetPropertiesAsync(properties);
- await encoder.FlushAsync();
- outputStream.Dispose();
- return photoStorage;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement