private async void ImageTool_Unloaded(object sender, RoutedEventArgs e) { if (TempImageFile != null) { await TempImageFile.DeleteAsync(); TempImageFile = null; } }
private async void OnTempImageFileChanged() { if (_isTemplateLoaded && SourceImageFile != null && TempImageFile != null) { scrollViewer.ZoomToFactor(1); // Ensure the stream is disposed once the image is loaded using (IRandomAccessStream fileStream = await TempImageFile.OpenAsync(Windows.Storage.FileAccessMode.Read)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); this.sourceImagePixelHeight = decoder.PixelHeight; this.sourceImagePixelWidth = decoder.PixelWidth; } if (this.sourceImagePixelHeight < 2 * CropSelection.MinSelectRegionSize || this.sourceImagePixelWidth < 2 * CropSelection.MinSelectRegionSize) { MessageDialog dialog = new MessageDialog("Image size is (" + sourceImagePixelWidth + "," + sourceImagePixelHeight + ") now and should be more than " + 2 * CropSelection.MinSelectRegionSize + " px"); await dialog.ShowAsync(); } 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; } ImageSource preSource = this.sourceImage.Source; this.sourceImage.Source = await BitmapHelper.GetCroppedBitmapAsync( this.TempImageFile, new Point(0, 0), new Size(this.sourceImagePixelWidth, this.sourceImagePixelHeight), sourceImageScale); if (preSource != null) { WriteableBitmap pre = preSource as WriteableBitmap; var source = this.sourceImage.Source as WriteableBitmap; if (pre.PixelWidth == source.PixelWidth && pre.PixelHeight == source.PixelHeight) { this.editImage.Source = this.sourceImage.Source; } } } } }