Example #1
0
        private Image GetCropedImage(Image imgSource, int width, int height)
        {
            bool isSizeChanged = false;
            // 計算截圖範圍
            int cropWidth  = imgSource.Width;
            int cropHeight = imgSource.Height;

            if (width < imgSource.Width && height < imgSource.Height)
            {
                if (1.0 * imgSource.Width / width * height <= imgSource.Height)
                {
                    cropHeight    = (int)(1.0 * imgSource.Width / width * height);
                    isSizeChanged = true;
                }
                else if (1.0 * imgSource.Height / height * width <= imgSource.Width)
                {
                    cropWidth     = (int)(1.0 * imgSource.Height / height * width);
                    isSizeChanged = true;
                }
            }
            else if (width < imgSource.Width && height >= imgSource.Height)
            {
                cropWidth     = (int)(1.0 * imgSource.Height / height * width);
                isSizeChanged = true;
            }
            else if (width >= imgSource.Width && height < imgSource.Height)
            {
                cropHeight    = (int)(1.0 * imgSource.Width / width * height);
                isSizeChanged = true;
            }
            else
            {
                // 原圖
            }

            // 進行截圖及縮圖
            if (isSizeChanged)
            {
                var cropedImage = ImageResizer.Crop(imgSource, cropWidth, cropHeight, AnchorPosition.Center);
                return(ImageResizer.ScaleByFixedSize(cropedImage, width, height));
            }


            return(null);
        }
Example #2
0
        private Image GetResizedImage(Image imgSource, int width, int height)
        {
            // 依比例縮圖
            // 計算大小
            if (width <= imgSource.Width && height >= (1.0 * imgSource.Height * width / imgSource.Width))
            {
                height = (int)(1.0 * imgSource.Height * width / imgSource.Width);
            }
            else if (height <= imgSource.Height && width >= (1.0 * imgSource.Width * height / imgSource.Height))
            {
                width = (int)(1.0 * imgSource.Width * height / imgSource.Height);
            }

            // 如果需要縮圖
            if (width < imgSource.Width || height < imgSource.Height)
            {
                return(ImageResizer.ScaleByFixedSize(imgSource, width, height));
            }

            return(null);
        }