/// <summary> /// Update preview image. /// </summary> async void UpdatePreviewImage() { double sourceImageWidthScale = imageCanvas.Width / this.sourceImagePixelWidth; double sourceImageHeightScale = imageCanvas.Height / this.sourceImagePixelHeight; Size previewImageSize = new Size( this.selectedRegion.SelectedRect.Width / sourceImageWidthScale, this.selectedRegion.SelectedRect.Height / sourceImageHeightScale); double previewImageScale = 1; if (previewImageSize.Width <= imageCanvas.Width && previewImageSize.Height <= imageCanvas.Height) { this.previewImage.Stretch = Windows.UI.Xaml.Media.Stretch.None; } else { this.previewImage.Stretch = Windows.UI.Xaml.Media.Stretch.Uniform; previewImageScale = Math.Min(imageCanvas.Width / previewImageSize.Width, imageCanvas.Height / previewImageSize.Height); } this.previewImage.Source = await CropBitmap.GetCroppedBitmapAsync( this.sourceImageFile, new Point(this.selectedRegion.SelectedRect.X / sourceImageWidthScale, this.selectedRegion.SelectedRect.Y / sourceImageHeightScale), previewImageSize, previewImageScale); }
/// <summary> /// Let user choose an image and load it. /// </summary> async private void openImageButton_Click(object sender, RoutedEventArgs e) { bool unsnapped = ((ApplicationView.Value != ApplicationViewState.Snapped) || ApplicationView.TryUnsnap()); if (!unsnapped) { NotifyUser("Cannot unsnap the sample."); return; } FileOpenPicker openPicker = new FileOpenPicker(); openPicker.ViewMode = PickerViewMode.Thumbnail; openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; openPicker.FileTypeFilter.Add(".jpg"); openPicker.FileTypeFilter.Add(".jpeg"); openPicker.FileTypeFilter.Add(".bmp"); openPicker.FileTypeFilter.Add(".png"); StorageFile imgFile = await openPicker.PickSingleFileAsync(); if (imgFile != null) { this.previewImage.Source = null; this.sourceImage.Source = null; this.imageCanvas.Visibility = Windows.UI.Xaml.Visibility.Collapsed; this.originalImageInfoText.Text = string.Empty; this.selectInfoInBitmapText.Text = string.Empty; this.saveImageButton.IsEnabled = false; // Ensure the stream is disposed once the image is loaded using (IRandomAccessStream fileStream = await imgFile.OpenAsync(Windows.Storage.FileAccessMode.Read)) { this.sourceImageFile = imgFile; BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); this.sourceImagePixelHeight = decoder.PixelHeight; this.sourceImagePixelWidth = decoder.PixelWidth; } if (this.sourceImagePixelHeight < 2 * this.CornerSize || this.sourceImagePixelWidth < 2 * this.CornerSize) { this.NotifyUser(string.Format("Please select an image which is larger than {0}*{0}", 2 * this.CornerSize)); return; } else { double sourceImageScale = 1; if (this.sourceImagePixelHeight < this.sourceImageGrid.ActualHeight && this.sourceImagePixelWidth < this.sourceImageGrid.ActualWidth) { this.sourceImage.Stretch = Windows.UI.Xaml.Media.Stretch.None; } else { sourceImageScale = Math.Min(this.sourceImageGrid.ActualWidth / this.sourceImagePixelWidth, this.sourceImageGrid.ActualHeight / this.sourceImagePixelHeight); this.sourceImage.Stretch = Windows.UI.Xaml.Media.Stretch.Uniform; } this.sourceImage.Source = await CropBitmap.GetCroppedBitmapAsync( this.sourceImageFile, new Point(0, 0), new Size(this.sourceImagePixelWidth, this.sourceImagePixelHeight), sourceImageScale); this.originalImageInfoText.Text = string.Format("Original Image Size: {0}*{1} ", this.sourceImagePixelWidth, this.sourceImagePixelHeight); } } }