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))); }
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); }
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); }
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); }
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); }
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); }
private static Image GetImageOfSize(Image originalImage, UserPhotoSize size) { Util.ThrowOnNullArgument(originalImage, "originalImage"); EnumValidator.ThrowIfInvalid <UserPhotoSize>(size, "size"); return(UserPhotoUtilities.GetScaledAndCroppedImage(originalImage, UserPhotoDimensions.GetImageSize(size))); }
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)); }