예제 #1
0
        /// <summary>
        /// This method will resize the in-memory image using a fixed ratio
        /// </summary>
        private void ImageSizeChanged(ResizeModes resizeMode)
        {
            var image = ImagePreview.Source as BitmapSource;

            if (image == null)
            {
                return;
            }

            Dispatcher.InvokeAsync(() =>
            {
                if (ImageHeight == (int)image.Height && ImageWidth == (int)image.Width)
                {
                    return;
                }

                using (var bitmap = WindowUtilities.BitmapSourceToBitmap(ImagePreview.Source as BitmapSource))
                {
                    if (bitmap == null)
                    {
                        StatusBar.ShowStatusError("No image to resize.");
                        return;
                    }

                    if (ImageWidth != bitmap.Width && ImageHeight != bitmap.Height)
                    {
                        return;
                    }


                    Bitmap bitmap2;
                    using (bitmap2 = ImageResizer.ResizeImageByMode(bitmap, ImageWidth, ImageHeight, resizeMode))
                    {
                        if (bitmap2 != null)
                        {
                            Debug.WriteLine($"ImageSizeChanged from: {ImageWidth} x {ImageHeight} to: {bitmap2.Width} x {bitmap2.Height}");


                            ImageWidth  = bitmap2.Width;
                            ImageHeight = bitmap2.Height;

                            var bmpSource = WindowUtilities.BitmapToBitmapSource(bitmap2);
                            WindowUtilities.DoEvents();
                            ImagePreview.Source = bmpSource;


                            Dispatcher.InvokeAsync(() => ResizeImagePreviewControl(bmpSource),
                                                   DispatcherPriority.ApplicationIdle);
                        }
                    }
                }
            }, DispatcherPriority.ApplicationIdle);
        }