Beispiel #1
0
        private BitmapSource ScaleToFill(int width, int height)
        {
            Contract.Requires(width > 0);
            Contract.Requires(height > 0);

            double heightRatio = height / (double)_orgBitMap.PixelHeight;
            double widthRatio  = width / (double)_orgBitMap.PixelWidth;

            BitmapSource bitmapSource;
            ImageSize    imageSize;

            if (heightRatio > widthRatio)
            {
                bitmapSource = ResizeImageByHeight(_imageBytes, height);
                var calc = new ImageSizeCalculator(bitmapSource.PixelWidth, height);
                imageSize = calc.ScaleToFill(width, height);
            }
            else
            {
                bitmapSource = ResizeImageByWidth(_imageBytes, width);
                var calc = new ImageSizeCalculator(width, bitmapSource.PixelHeight);
                imageSize = calc.ScaleToFill(width, height);
            }

            var croppedBitmap = new CroppedBitmap(bitmapSource, new Int32Rect(imageSize.XOffset, imageSize.YOffset, imageSize.Width, imageSize.Height));

            return(croppedBitmap);
        }
        private BitmapSource ScaleToFill(int width, int height)
        {
            Contract.Requires(width > 0);
            Contract.Requires(height > 0);

            double heightRatio = height / (double)_orgBitMap.PixelHeight;
            double widthRatio = width / (double)_orgBitMap.PixelWidth;

            BitmapSource bitmapSource;
            ImageSize imageSize;
            
            if (heightRatio > widthRatio)
            {
                bitmapSource = ResizeImageByHeight(_imageBytes, height);
                var calc = new ImageSizeCalculator(bitmapSource.PixelWidth, height);
                imageSize = calc.ScaleToFill(width, height);
            }
            else
            {
                bitmapSource = ResizeImageByWidth(_imageBytes, width);
                var calc = new ImageSizeCalculator(width, bitmapSource.PixelHeight);
                imageSize = calc.ScaleToFill(width, height);
            }

            var croppedBitmap = new CroppedBitmap(bitmapSource, new Int32Rect(imageSize.XOffset, imageSize.YOffset, imageSize.Width, imageSize.Height));
            return croppedBitmap;
        }