示例#1
0
        private static Size GetTargetSize(this ImageResizeRequestModel request, Size originalSize)
        {
            var maxWidth = request.GetMaxWidth(originalSize);
            var maxHeight = request.GetMaxHeight(originalSize);
            var aspectRatio = originalSize.GetAspectRatio();
            Size targetSize;

            // if image is taller than it is wide, aspect ratio will be less than 1
            // in these cases, adjust the width based on the height
            // also take this approach when the image is perfectly square (aspect ratio == 1.0)
            if (aspectRatio <= 1)
            {
                targetSize = originalSize.ReduceByHeight(maxHeight);

                // width can still be greater than maxWidth
                if (targetSize.Width > maxWidth)
                    // now we have to reduce the height based on the max width
                    targetSize = originalSize.ReduceByWidth(maxWidth);
            }
            // if image is wider than it is tall, aspect ratio will be greater than 1
            // in these cases, adjust the height based on the width
            else
            {
                targetSize = originalSize.ReduceByWidth(maxWidth);
                // height can still be greater than maxHeight
                if (targetSize.Height > maxHeight)
                    // now we have to reduce the width based on the max height
                    targetSize = originalSize.ReduceByHeight(maxHeight);
            }

            return targetSize;
        }