private async void OnSourceImageFileChanged(StorageFile newFile) { if (_isTemplateLoaded && newFile != null && this.ActualWidth > 0 && this.ActualHeight > 0) { await ImageHelper.StorageFileToStoragefileWithRightDirection(newFile); // Ensure the stream is disposed once the image is loaded using (IRandomAccessStream fileStream = await newFile.OpenAsync(Windows.Storage.FileAccessMode.Read)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); this.sourceImagePixelHeight = decoder.PixelHeight; this.sourceImagePixelWidth = decoder.PixelWidth; } if (this.sourceImagePixelHeight < 2 * 30 || this.sourceImagePixelWidth < 2 * 30) { } else { double sourceImageScale = 1; if (this.sourceImagePixelHeight < this.ActualHeight && this.sourceImagePixelWidth < this.ActualWidth) { this.sourceImage.Stretch = Windows.UI.Xaml.Media.Stretch.None; } else { sourceImageScale = Math.Min(this.ActualWidth / this.sourceImagePixelWidth, this.ActualHeight / this.sourceImagePixelHeight); this.sourceImage.Stretch = Windows.UI.Xaml.Media.Stretch.Uniform; } this.sourceImage.ImageOpened += SourceImage_ImageOpened; this.sourceImage.Source = await BitmapHelper.GetCroppedBitmapAsync( newFile, new Point(0, 0), new Size(this.sourceImagePixelWidth, this.sourceImagePixelHeight), sourceImageScale); await Task.Delay(100); ReSetSelectionRect(); } } }