Exemple #1
0
        public static void ScaleImage(Bitmap image, FixedDimension? fixedDimension, int maxDimension, Stream saveTo)
        {
            Size imageSize = CalculateSize(image.Size, fixedDimension, maxDimension);

            Bitmap thumbnailImage = new Bitmap(imageSize.Width, imageSize.Height);
            Graphics graphics = Graphics.FromImage(thumbnailImage);
            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            graphics.DrawImage(image, 0, 0, thumbnailImage.Width, thumbnailImage.Height);
            thumbnailImage.Save(saveTo, System.Drawing.Imaging.ImageFormat.Jpeg);
            saveTo.Position = 0;
        }
Exemple #2
0
        private static Size CalculateSize(Size image, FixedDimension? fixedDimension, int maxDimension)
        {
            int width;
            int height;
            if ((fixedDimension.HasValue && fixedDimension.Value == FixedDimension.Width) || (image.Width > image.Height))
            {
                width = maxDimension;
                height = (maxDimension * image.Height) / image.Width;

            }
            else if ((fixedDimension.HasValue && fixedDimension.Value == FixedDimension.Height) || (image.Height > image.Width))
            {
                height = maxDimension;
                width = (maxDimension * image.Width) / image.Height;
            }
            else
                width = height = maxDimension;

            return new Size(width, height);
        }