C# 圖片壓縮

        /// <summary>  
        /// 按質量比和尺寸收縮唄數,生成縮略圖,保存爲另外路勁
        /// </summary>  
        /// <param name="sourceFile">原始圖片文件</param>  
        /// <param name="quality">質量壓縮比</param>  
        /// <param name="multiple">收縮倍數</param>  
        /// <param name="outputFile">輸出文件名</param>  
        /// <returns>成功返回true,失敗則返回false</returns>  
        public static bool getThumImage(String sourceFile, long quality, int multiple, String outputFile)
        {
            bool flag = false;
            try
            {
                flag = ThumImage(sourceFile, quality, multiple, outputFile);
            }
            catch
            {
                flag = false;
            }
            return flag;
        }

        /**/
        /// <summary>  
        /// 按質量比和尺寸收縮唄數,生成縮略圖;保存在同一路徑
        /// </summary>  
        /// <param name="sourceFile">原始圖片文件</param>  
        /// <param name="quality">質量壓縮比</param>  
        /// <param name="multiple">收縮倍數</param>  
        /// <param name="outputFile">輸出文件名</param>  
        /// <returns>成功返回true,失敗則返回false</returns>  
        public static bool getThumImage(String sourceFile, long quality, int multiple)
        {
            bool flag = false;
            try
            {
                flag = ThumImage(sourceFile, quality, multiple, sourceFile);
            }
            catch
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>  
        /// 按圖片尺寸大小壓縮圖片  ,保存在不一樣路徑
        /// </summary>  
        /// <param name="sourceFile">原始圖片文件</param>  
        /// <param name="multiple">收縮倍數</param>  
        /// <returns>成功返回true,失敗則返回false</returns>  
        public static bool ImageByMultiple(String sourceFile, int multiple, String outputFile)
        {
            bool flag = false;
            try
            {
                flag = ThumImageByMultiple(sourceFile, multiple, outputFile);
            }
            catch
            {
                flag = false;
            }
            return flag;
        }


        /// <summary>  
        ///  按圖片尺寸大小壓縮圖片  ,保存在同一路徑
        /// </summary>  
        /// <param name="multiple">收縮倍數</param>  
        /// <returns>成功返回true,失敗則返回false</returns>  
        public static bool ImageByMultiple(String sourceFile, int multiple)
        {
            bool flag = false;
            try
            {
                flag = ThumImageByMultiple(sourceFile, multiple, sourceFile);
            }
            catch
            {
                flag = false;
            }
            return flag;
        }


        /// <summary>  
        /// 質量壓縮比,保存在不一樣一路徑
        /// </summary>  
        /// <param name="sourceFile">原始圖片文件</param>  
        /// <param name="quality">質量壓縮比</param>  
        /// <returns>成功返回true,失敗則返回false</returns>  
        public static bool ImageByQuality(String sourceFile, int quality, String outputFile)
        {
            bool flag = false;
            try
            {
                flag = ThumImageByQuality(sourceFile, quality, outputFile);
            }
            catch
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>  
        /// 按質量壓縮比壓縮圖片,保存在同一路徑
        /// </summary>  
        /// <param name="sourceFile">原始圖片文件</param>  
        /// <param name="quality">質量壓縮比</param>  
        /// <returns>成功返回true,失敗則返回false</returns>  
        public static bool ImageByQuality(String sourceFile, int quality)
        {
            bool flag = false;
            try
            {
                flag = ThumImageByQuality(sourceFile, quality, sourceFile);
            }
            catch
            {
                flag = false;
            }
            return flag;
        }
        #endregion

        #region 調用圖片處理具體方法


        /// <summary>
        /// 按照圖片質量生成圖片,
        /// </summary>
        /// <param name="sourceFile">原始圖片文件</param>  
        /// <param name="quality">質量壓縮比</param>  
        /// <param name="outputFile">輸出文件名</param>  
        /// <returns>成功返回true,失敗則返回false</returns>  
        private static bool ThumImageByQuality(String sourceFile, long quality, String outputFile)
        {
            bool flag = false;
            try
            {
                long imageQuality = quality;
                Bitmap sourceImage = new Bitmap(sourceFile);
                ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");
                System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
                EncoderParameters myEncoderParameters = new EncoderParameters(1);
                EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality);
                myEncoderParameters.Param[0] = myEncoderParameter;
                float xWidth = sourceImage.Width;
                float yWidth = sourceImage.Height;
                Bitmap newImage = new Bitmap((int)(xWidth), (int)(yWidth));
                Graphics g = Graphics.FromImage(newImage);
                g.DrawImage(sourceImage, 0, 0, xWidth, yWidth);
                sourceImage.Dispose();
                g.Dispose();
                newImage.Save(outputFile, myImageCodecInfo, myEncoderParameters);
                flag = true;
            }
            catch
            {
                flag = false;
            }
            return flag;

        }


        /// <summary>
        /// 生成縮略圖
        /// </summary>
        /// <param name="sourceFile">原始圖片文件</param>  
        /// <param name="quality">質量壓縮比</param>  
        /// <param name="multiple">收縮倍數</param>  
        /// <param name="outputFile">輸出文件名</param>  
        /// <returns>成功返回true,失敗則返回false</returns>  
        private static bool ThumImage(String sourceFile, long quality, int multiple, String outputFile)
        {
            bool flag = false;
            try
            {
                long imageQuality = quality;
                Bitmap sourceImage = new Bitmap(sourceFile);
                ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");
                System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
                EncoderParameters myEncoderParameters = new EncoderParameters(1);
                EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality);
                myEncoderParameters.Param[0] = myEncoderParameter;
                float xWidth = sourceImage.Width;
                float yWidth = sourceImage.Height;
                Bitmap newImage = new Bitmap((int)(xWidth / multiple), (int)(yWidth / multiple));
                Graphics g = Graphics.FromImage(newImage);
                g.DrawImage(sourceImage, 0, 0, xWidth / multiple, yWidth / multiple);
                sourceImage.Dispose();
                g.Dispose();
                newImage.Save(outputFile, myImageCodecInfo, myEncoderParameters);
                flag = true;
            }
            catch
            {
                flag = false;
            }
            return flag;

        }

        /// <summary>  
        /// 按圖片尺寸大小壓縮圖片  
        /// </summary>  
        /// <param name="sourceFile">原始圖片文件</param>  
        /// <param name="multiple">收縮倍數</param>  
        /// <param name="outputFile">輸出文件名</param>  
        /// <returns>成功返回true,失敗則返回false</returns>  
        public static bool ThumImageByMultiple(String sourceFile, int multiple, String outputFile)
        {
            try
            {
                Bitmap sourceImage = new Bitmap(sourceFile);
                ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");
                float xWidth = sourceImage.Width;
                float yWidth = sourceImage.Height;
                Bitmap newImage = new Bitmap((int)(xWidth / multiple), (int)(yWidth / multiple));
                Graphics g = Graphics.FromImage(newImage);
                g.DrawImage(sourceImage, 0, 0, xWidth / multiple, yWidth / multiple);
                sourceImage.Dispose();
                g.Dispose();
                newImage.Save(outputFile);
                return true;
            }
            catch
            {
                return false;
            }
        }



        /// <summary>  
        /// 按圖片尺寸大小壓縮圖片  
        /// </summary>  
        /// <param name="sourceFile">原始圖片文件</param>  
        /// <param name="multiple">收縮倍數</param>  
        /// <param name="outputFile">輸出文件名</param>  
        /// <returns>成功返回true,失敗則返回false</returns>  
        public static bool FixSize(String sourceFile, int xWidth, int yWidth, String outputFile)
        {
            try
            {
                Bitmap sourceImage = new Bitmap(sourceFile);
                ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");
                Bitmap newImage = new Bitmap((int)(xWidth), (int)(yWidth));
                Graphics g = Graphics.FromImage(newImage);
                g.DrawImage(sourceImage, 0, 0, xWidth, yWidth);
                sourceImage.Dispose();
                g.Dispose();
                newImage.Save(outputFile);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion getThumImage


        /// <summary>  
        /// 獲取圖片編碼信息  
        /// </summary>  
        private static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }
相關文章
相關標籤/搜索