1. 程式人生 > >C#生成縮圖,指定畫素大小

C#生成縮圖,指定畫素大小

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImgChuLi
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = "C:\\1122.jpg";
            string filePathS = "C:\\1122_TW.jpg";//縮圖地址


            long quality = 75;   //圖片質量,優選75質量,儲存比較小
            SendSmallImage(filePath, filePathS, 240, 240, quality, "CUT");
        }

        /// <summary>
        /// 得到縮圖,指定畫素
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="newFile"></param>
        /// <param name="maxHeight"></param>
        /// <param name="maxWidth"></param>
        /// <param name="qualityNum">圖片質量1-100</param>
        /// <param name="mode">縮放模式,CUT裁剪不失真,HW定寬高有可能變形,W定寬,H定高</param>
        public static void SendSmallImage(string fileName, string newFile, int maxHeight, int maxWidth, long qualityNum, string mode)
        {
            Image img = Image.FromFile(fileName);
            System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
            int towidth = maxWidth;
            int toheight = maxHeight;
            int x = 0;
            int y = 0;
            int ow = img.Width;
            int oh = img.Height;
            switch (mode)
            {
                case "HW"://指定高寬縮放(可能變形)                
                    break;
                case "W"://指定寬,高按比例                    
                    toheight = img.Height * maxWidth / img.Width;
                    break;
                case "H"://指定高,寬按比例
                    towidth = img.Width * maxHeight / img.Height;
                    break;
                case "CUT"://指定高寬裁減(不變形)                
                    if ((double)img.Width / (double)img.Height > (double)towidth / (double)toheight)
                    {
                        oh = img.Height;
                        ow = img.Height * towidth / toheight;
                        y = 0;
                        x = (img.Width - ow) / 2;
                    }
                    else
                    {
                        ow = img.Width;
                        oh = img.Width * maxHeight / towidth;
                        x = 0;
                        y = (img.Height - oh) / 2;
                    }
                    break;
                default:
                    break;
            }
            Bitmap outBmp = new Bitmap(towidth, toheight);
            Graphics g = Graphics.FromImage(outBmp);
            // 設定畫布的描繪質量
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(img, new Rectangle(0, 0, towidth, toheight), x, y, ow, oh, GraphicsUnit.Pixel);
            g.Dispose();
            // 以下程式碼為儲存圖片時,設定壓縮質量
            EncoderParameters encoderParams = new EncoderParameters();
            long[] quality = new long[1];
            quality[0] = qualityNum;//圖片質量1-100
            EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
            encoderParams.Param[0] = encoderParam;
            //獲得包含有關內建影象編碼解碼器的資訊的ImageCodecInfo 物件.
            ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo jpegICI = null;
            for (int index = 0; index < arrayICI.Length; index++)
            {
                if (arrayICI[index].FormatDescription.Equals("JPEG"))
                {
                    jpegICI = arrayICI[index];
                    //設定JPEG編碼
                    break;
                }
            }
            if (jpegICI != null)
            {
                outBmp.Save(newFile, jpegICI, encoderParams);
            }
            else
            {
                outBmp.Save(newFile, thisFormat);
            }
            img.Dispose();
            outBmp.Dispose();
        }
    }
}