private async Task<StorageFile> RotateImage(int rotation, IStorageFile file) { var data = await FileIO.ReadBufferAsync(file); var ms = new InMemoryRandomAccessStream(); var dw = new DataWriter(ms); dw.WriteBuffer(data); await dw.StoreAsync(); ms.Seek(0); var bm = new BitmapImage(); await bm.SetSourceAsync(ms); var wb = new WriteableBitmap(bm.PixelHeight, bm.PixelWidth); ms.Seek(0); await wb.SetSourceAsync(ms); var rotated = wb.Rotate(rotation); var result = await this.SaveCroppedImage(rotated); return result; }
/// <summary> /// Gets the current preview frame as a SoftwareBitmap, displays its properties in a TextBlock, and can optionally display the image /// in the UI and/or save it to disk as a jpg /// </summary> /// <returns>Task representing the async event status</returns> private async Task GetPreviewFrameAsSoftwareBitmapAsync() { // Get information about the preview VideoEncodingProperties previewProperties = this._mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties; // Create the video frame to request a SoftwareBitmap preview frame VideoFrame videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height); // Capture the preview frame using (VideoFrame currentFrame = await this._mediaCapture.GetPreviewFrameAsync(videoFrame)) { // Collect the resulting frame SoftwareBitmap previewFrame = currentFrame.SoftwareBitmap; // Show the frame information Debug.WriteLine("{0}x{1} {2}", previewFrame.PixelWidth, previewFrame.PixelHeight, previewFrame.BitmapPixelFormat); tempWriteableBitmap = new WriteableBitmap(previewFrame.PixelWidth, previewFrame.PixelHeight); previewFrame.CopyToBuffer(tempWriteableBitmap.PixelBuffer); // Crop to a square, based on the smallest side int minEdge = Math.Min(tempWriteableBitmap.PixelWidth, tempWriteableBitmap.PixelHeight); tempWriteableBitmap = tempWriteableBitmap .Crop(0, 0, minEdge, minEdge) .Resize(App.LedMatrix.PixelWidth, App.LedMatrix.PixelHeight, WriteableBitmapExtensions.Interpolation.Bilinear); WriteableBitmap previewFrameImageSource = tempWriteableBitmap.Rotate(90).Resize( (int)this.postViewbox.Height, (int)this.postViewbox.Width, WriteableBitmapExtensions.Interpolation.NearestNeighbor); this.previewFrameImage.Source = previewFrameImageSource; } }