Monday, 2 May 2016


public static int[] CalculateImageDimension(int srcWidth, int srcHeight, int maxWidth, int maxHeight)
        {
            double resizeWidth = 0.00, resizeHeight = 0.00;
            double maxAspect = (double)maxWidth / (double)maxHeight;
            double aspect = (double)srcWidth / (double)srcHeight;

            if (maxAspect > aspect && srcWidth > maxWidth)
            {
                //Width is the bigger dimension relative to max bounds
                resizeWidth = maxWidth;
                resizeHeight = maxWidth / aspect;
            }
            else if (maxAspect <= aspect && srcHeight > maxHeight)
            {
                //Height is the bigger dimension
                resizeHeight = maxHeight;
                resizeWidth = maxHeight * aspect;
            }
            else
            {
                resizeHeight = maxHeight;
                resizeWidth = maxWidth;
            }
            int[] heightWidth = new[] { Convert.ToInt32(resizeWidth), Convert.ToInt32(resizeHeight) };
            return heightWidth;
        }

No comments:

Post a Comment