private static void UpdateCornerImage(Image image, Grid imageHolder, BitmapSource bitmap, RegionChooser regionChooser, bool isRetry = false) { // Image dimensions int imageWidth = bitmap.PixelWidth; int imageHeight = bitmap.PixelHeight; double availableWidth = imageHolder.ActualWidth; double availableHeight = imageHolder.ActualHeight; if (availableWidth == 0 || availableHeight == 0) { // Intermittent bug causing this. In this case we queue it up to go again after a layout pass has been done. if (!isRetry) { DispatchUtilities.BeginInvoke(() => { UpdateCornerImage(image, imageHolder, bitmap, regionChooser, isRetry: true); }); } return; } int cornerWidthPixels = (int)(availableWidth / ZoomedPixelSize); int cornerHeightPixels = (int)(availableHeight / ZoomedPixelSize); // Make sure the subsection of the image is not larger than the image itself cornerWidthPixels = Math.Min(cornerWidthPixels, imageWidth); cornerHeightPixels = Math.Min(cornerHeightPixels, imageHeight); double cornerWidth = cornerWidthPixels * ZoomedPixelSize; double cornerHeight = cornerHeightPixels * ZoomedPixelSize; var croppedBitmap = new CroppedBitmap(); croppedBitmap.BeginInit(); croppedBitmap.SourceRect = regionChooser(imageWidth, imageHeight, cornerWidthPixels, cornerHeightPixels); croppedBitmap.Source = bitmap; croppedBitmap.EndInit(); image.Source = croppedBitmap; image.Width = cornerWidth; image.Height = cornerHeight; }
private static void UpdateCornerImage(Image image, Grid imageHolder, BitmapSource bitmap, RegionChooser regionChooser, bool isRetry = false) { // Image dimensions int imageWidth = bitmap.PixelWidth; int imageHeight = bitmap.PixelHeight; double availableWidth = imageHolder.ActualWidth; double availableHeight = imageHolder.ActualHeight; if (availableWidth == 0 || availableHeight == 0) { // Intermittent bug causing this. In this case we queue it up to go again after a layout pass has been done. if (!isRetry) { DispatchService.BeginInvoke(() => { UpdateCornerImage(image, imageHolder, bitmap, regionChooser, isRetry: true); }); } return; } int cornerWidthPixels = (int)(availableWidth / ZoomedPixelSize); int cornerHeightPixels = (int)(availableHeight / ZoomedPixelSize); // Make sure the subsection of the image is not larger than the image itself cornerWidthPixels = Math.Min(cornerWidthPixels, imageWidth); cornerHeightPixels = Math.Min(cornerHeightPixels, imageHeight); double cornerWidth = cornerWidthPixels * ZoomedPixelSize; double cornerHeight = cornerHeightPixels * ZoomedPixelSize; var croppedBitmap = new CroppedBitmap(); croppedBitmap.BeginInit(); croppedBitmap.SourceRect = regionChooser(imageWidth, imageHeight, cornerWidthPixels, cornerHeightPixels); croppedBitmap.Source = bitmap; croppedBitmap.EndInit(); image.Source = croppedBitmap; image.Width = cornerWidth; image.Height = cornerHeight; }