C#高质量(清晰)缩略图生成类

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace ConsoleApplication1
{
    public class ImageHelper
    {
        /// <summary>
        /// 生成缩略图(缩小原图到指定范围)
        /// </summary>
        /// <param name="srcFile">原图路径</param>
        /// <param name="destFile">缩略图路径</param>
        /// <param name="maxWidth">缩略图最大宽度</param>
        /// <param name="maxHeight">缩略图最大高度</param>
        /// <returns></returns>
        public static bool GetFixedRatioImage(string srcFile, string destFile, int maxWidth, int maxHeight)
        {
            if (!File.Exists(srcFile))
                return false;

            int destWidth = 0, destHeight = 0;

            using (Image image = Image.FromFile(srcFile))
            {          
                if (image.Width > maxWidth && image.Height > maxHeight)
                {
                    destWidth = maxWidth;
                    destHeight = maxWidth * image.Height / image.Width;

                    if (destHeight > maxHeight)
                    {
                        destWidth = maxHeight * image.Width / image.Height;
                        destHeight = maxHeight;
                    }
                }
                else
                {
                    destWidth = image.Width;
                    destHeight = image.Height;
                }

                using (Image bitmap = new Bitmap(destWidth, destHeight))
                {
                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
                        graphics.InterpolationMode = InterpolationMode.High;
                        graphics.SmoothingMode = SmoothingMode.HighQuality;
                        graphics.Clear(Color.Transparent);
                        graphics.DrawImage(image, new Rectangle(0, 0, destWidth, destHeight), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
                        bitmap.Save(destFile, ImageFormat.Jpeg);
                    }
                }
            }

            return true;
        }

        /// <summary>
        /// 生成缩略图(裁切原图并缩小到指定尺寸)
        /// </summary>
        /// <param name="srcFile">原图路径</param>
        /// <param name="destFile">缩略图路径</param>
        /// <param name="destWidth">缩略图宽度</param>
        /// <param name="destHeight">缩略图高度</param>
        /// <returns></returns>
        public static bool GetFixedSizeImage(string srcFile, string destFile, int destWidth, int destHeight)
        {
            if (!File.Exists(srcFile))
                return false;

            int srcWidth = 0, srcHeight = 0;

            using (Image image = Image.FromFile(srcFile))
            {
                if (image.Width > destWidth && image.Height > destHeight)
                {
                    if ((float)image.Width / (float)destWidth > (float)image.Height / (float)destHeight)
                    {
                        srcWidth = image.Height * destWidth / destHeight;
                        srcHeight = image.Height;
                    }
                    else
                    {
                        srcWidth = image.Width;
                        srcHeight = image.Width * destHeight / destWidth;
                    }
                }
                else
                {                    
                    srcWidth = image.Width;                    
                    srcHeight = image.Height;
                    destWidth = image.Width;
                    destHeight = image.Height;
                }

                using (Image bitmap = new Bitmap(destWidth, destHeight))
                {
                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
                        graphics.InterpolationMode = InterpolationMode.High;
                        graphics.SmoothingMode = SmoothingMode.HighQuality;
                        graphics.Clear(Color.Transparent);
                        graphics.DrawImage(image, new Rectangle(0, 0, destWidth, destHeight), 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
                        bitmap.Save(destFile, ImageFormat.Jpeg);
                    }
                }
            }

            return true;
        }
    }
}


上一篇: 机动车科目二考试Flash视频
下一篇: #杨梅#成熟了
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
评论: 0 | 引用: 0 | 查看次数: 4077
发表评论
登录后再发表评论!