Exemplo n.º 1
0
        private static RectangleF GetImageCenterCrop(Size imageSize, Size maxImageSize)
        {
            PointF imageCenterPosition = UserPhotoUtilities.GetImageCenterPosition(imageSize, maxImageSize);
            float  width  = (float)Math.Min(imageSize.Width, maxImageSize.Width);
            float  height = (float)Math.Min(imageSize.Height, maxImageSize.Height);

            return(new RectangleF(imageCenterPosition, new SizeF(width, height)));
        }
Exemplo n.º 2
0
        private static float GetHorizontalScaleFactor(Size imageSize, Size maxImageSize)
        {
            float result = 1f;

            if (UserPhotoUtilities.NeedsHorizontalCrop(imageSize, maxImageSize))
            {
                result = (float)maxImageSize.Width / (float)imageSize.Width;
            }
            return(result);
        }
Exemplo n.º 3
0
        private static float GetVerticalScaleFactor(Size imageSize, Size maxImageSize)
        {
            float result = 1f;

            if (UserPhotoUtilities.NeedsVerticalCrop(imageSize, maxImageSize))
            {
                result = (float)maxImageSize.Height / (float)imageSize.Height;
            }
            return(result);
        }
Exemplo n.º 4
0
        private static PointF GetImageCenterPosition(Size imageSize, Size maxImageSize)
        {
            PointF result = new PointF(0f, 0f);

            if (UserPhotoUtilities.NeedsHorizontalCrop(imageSize, maxImageSize))
            {
                result.X = (float)(imageSize.Width - maxImageSize.Width) * 0.5f;
            }
            if (UserPhotoUtilities.NeedsVerticalCrop(imageSize, maxImageSize))
            {
                result.Y = (float)(imageSize.Height - maxImageSize.Height) * 0.35f;
            }
            return(result);
        }
Exemplo n.º 5
0
        public static Dictionary <UserPhotoSize, Image> GetAllScaleCroppedImages(Stream originalImageStream)
        {
            Util.ThrowOnNullArgument(originalImageStream, "originalImageStream");
            Dictionary <UserPhotoSize, Image> result;

            using (Image imageFromStream = UserPhotoUtilities.GetImageFromStream(originalImageStream, true))
            {
                Dictionary <UserPhotoSize, Image> dictionary = new Dictionary <UserPhotoSize, Image>();
                foreach (UserPhotoSize userPhotoSize in (UserPhotoSize[])Enum.GetValues(typeof(UserPhotoSize)))
                {
                    dictionary.Add(userPhotoSize, UserPhotoUtilities.GetImageOfSize(imageFromStream, userPhotoSize));
                }
                result = dictionary;
            }
            return(result);
        }
Exemplo n.º 6
0
        private static Image GetScaledAndCroppedImage(Image image, Size maxImageSize)
        {
            if (maxImageSize.Width > image.Size.Width || maxImageSize.Height > image.Size.Height)
            {
                return(null);
            }
            Size  imageScaledSize = UserPhotoUtilities.GetImageScaledSize(image.Size, maxImageSize);
            Image result;

            using (Bitmap bitmap = new Bitmap(imageScaledSize.Width, imageScaledSize.Height))
            {
                using (Graphics graphics = Graphics.FromImage(bitmap))
                {
                    graphics.CompositingQuality = CompositingQuality.HighQuality;
                    graphics.SmoothingMode      = SmoothingMode.HighQuality;
                    graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                    graphics.DrawImage(image, 0, 0, bitmap.Width, bitmap.Height);
                }
                RectangleF imageCenterCrop = UserPhotoUtilities.GetImageCenterCrop(imageScaledSize, maxImageSize);
                result = bitmap.Clone(imageCenterCrop, bitmap.PixelFormat);
            }
            return(result);
        }
Exemplo n.º 7
0
 private static Image GetImageOfSize(Image originalImage, UserPhotoSize size)
 {
     Util.ThrowOnNullArgument(originalImage, "originalImage");
     EnumValidator.ThrowIfInvalid <UserPhotoSize>(size, "size");
     return(UserPhotoUtilities.GetScaledAndCroppedImage(originalImage, UserPhotoDimensions.GetImageSize(size)));
 }
Exemplo n.º 8
0
        private static Size GetImageScaledSize(Size imageSize, Size maxImageSize)
        {
            float scale = Math.Max(UserPhotoUtilities.GetVerticalScaleFactor(imageSize, maxImageSize), UserPhotoUtilities.GetHorizontalScaleFactor(imageSize, maxImageSize));

            return(UserPhotoUtilities.GetScaledSize(imageSize, scale));
        }