Exemple #1
0
        static void Preview(HttpContext context, PreviewImageMode mode)
        {
            int    width, height;
            string url, filepath;

            Ready(context, out width, out height, out filepath);
            var    rootPath      = context.Server.MapPath("~/");
            string thumbnailPath = context.Server.MapPath(ThumbnailLocation) + filepath.Substring(rootPath.Length, filepath.Length - rootPath.Length);

            thumbnailPath = thumbnailPath + "_" + width + "_" + height + "_" + Enum.GetName(modeType, mode) + ".jpg";
            if (File.Exists(thumbnailPath))
            {
                OutputImage(context, thumbnailPath);
            }
            else
            {
                using (var bitmap = LoadFile(filepath))
                {
                    using (var newImage = GenerateThumbnail(bitmap, mode, width, height))
                    {
                        OutputImage(context, newImage);

                        SaveFile(thumbnailPath, newImage);
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// 生成缩略图核心逻辑
        /// </summary>
        /// <param name="oldImage">源图片</param>
        /// <param name="mode">缩放模式</param>
        /// <param name="newWidth">宽度</param>
        /// <param name="newHeight">高度</param>
        /// <returns></returns>
        public static Bitmap GenerateThumbnail(Bitmap oldImage, PreviewImageMode mode, int newWidth, int newHeight)
        {
            if (newWidth <= 0 || newHeight <= 0)
            {
                throw new ArgumentOutOfRangeException("newWidth 或 newHeight不能小于0");
            }
            int    oldWidth = oldImage.Width, oldHeight = oldImage.Height;
            Bitmap newImage          = new Bitmap(newWidth, newHeight);
            var    interpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            switch (mode)
            {
            case PreviewImageMode.Cut:    //裁剪
                if (oldWidth * newHeight > oldHeight * newWidth)
                {
                    oldWidth = oldHeight * newWidth / newHeight;
                }
                else if (oldWidth * newHeight < oldHeight * newWidth)
                {
                    oldHeight = oldWidth * newHeight / newWidth;
                }
                using (Graphics g = Graphics.FromImage(newImage))
                {
                    g.InterpolationMode = interpolationMode;
                    g.DrawImage(oldImage, new Rectangle(0, 0, newImage.Width, newImage.Height), new Rectangle((oldImage.Width - oldWidth) / 2, (oldImage.Height - oldHeight) / 2, oldWidth, oldHeight), GraphicsUnit.Pixel);
                }
                break;

            case PreviewImageMode.LeaveWhite:    //留白
                int w = newWidth, h = newHeight;
                if (oldWidth * newHeight > oldHeight * newWidth)
                {
                    h = oldHeight * newWidth / oldWidth;
                }
                else if (oldWidth * newHeight < oldHeight * newWidth)
                {
                    w = oldWidth * newHeight / oldHeight;
                }
                using (Graphics g = Graphics.FromImage(newImage))
                {
                    g.InterpolationMode = interpolationMode;
                    g.FillRectangle(new SolidBrush(LeaveWhiteColor), new Rectangle(0, 0, newImage.Width, newImage.Height));
                    g.DrawImage(oldImage, new Rectangle((newImage.Width - w) / 2, (newImage.Height - h) / 2, w, h),
                                new Rectangle(0, 0, oldImage.Width, oldImage.Height),
                                GraphicsUnit.Pixel);
                }
                break;

            default:
                using (Graphics g = Graphics.FromImage(newImage))
                {
                    g.InterpolationMode = interpolationMode;
                    g.DrawImage(oldImage, new Rectangle(0, 0, newWidth, newHeight));
                }
                break;
            }
            return(newImage);
        }