C#實現圖片壓縮

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;

namespace CScreen
{
    public class PhotoReduce
    {

        public Bitmap ystp(int int_Width, int int_Height, Image
 image) //壓縮圖片
        {


            System.Drawing.Image oldimage = new System.Drawing.Bitmap(image);
            float New_Width; // 新的寬度    
            float New_Height; // 新的高度    
            float Old_Width, Old_Height; //原始高寬    
            int flat = 0;//標記圖片是不是等比    


            int xPoint = 0;//若果要補白邊的話,原圖像所在的x,y座標。    
            int yPoint = 0;
            //判斷圖片    

            Old_Width = (float)oldimage.Width;
            Old_Height = (float)oldimage.Height;

            if ((Old_Width / Old_Height) > ((float)int_Width / (float)int_Height)) //當圖片太寬的時候    
            {
                New_Height = Old_Height * ((float)int_Width / (float)Old_Width);
                New_Width = (float)int_Width;
                //此時x座標不用修改    
                yPoint = (int)(((float)int_Height - New_Height) / 2);
                flat = 1;
            }
            else if ((oldimage.Width / oldimage.Height) == ((float)int_Width / (float)int_Height))
            {
                New_Width = int_Width;
                New_Height = int_Height;
            }
            else
            {
                New_Width = (int)oldimage.Width * ((float)int_Height / (float)oldimage.Height);//太高的時候   
                New_Height = int_Height;
                //此時y座標不用修改    
                xPoint = (int)(((float)int_Width - New_Width) / 2);
                flat = 1;
            }

            // ===縮小圖片===    
            System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage((int)New_Width, (int)New_Height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
            Bitmap bm = new Bitmap(thumbnailImage);

            if (flat != 0)
            {
                Bitmap bmOutput = new Bitmap(int_Width, int_Height);
                Graphics gc = Graphics.FromImage(bmOutput);
                SolidBrush tbBg = new SolidBrush(Color.White);
                gc.FillRectangle(tbBg, 0, 0, int_Width, int_Height); //填充爲白色    


                gc.DrawImage(bm, xPoint, yPoint, (int)New_Width, (int)New_Height);
                Bitmap bmp = new Bitmap(bmOutput);

                //ImageCoderInfo  
                ImageCodecInfo ici = null;
                //Encoder
                Encoder ecd = null;
                //EncoderParameter
                EncoderParameter ept = null;
                //EncoderParameters
                EncoderParameters eptS = null;
                try
                {
                    //bmp = new Bitmap(filePath);
                    ici = this.getImageCoderInfo("image/jpeg");
                    ecd = Encoder.Quality;
                    eptS = new EncoderParameters(1);
                    ept = new EncoderParameter(ecd, 10L);
                    eptS.Param[0] = ept;
                    //bmp.Save(filePath_ystp, ici, eptS);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    bmp.Dispose();
                    ept.Dispose();
                    eptS.Dispose();
                }

            }
            else
            {

                //ImageCoderInfo  
                ImageCodecInfo ici = null;
                //Encoder
                Encoder ecd = null;
                //EncoderParameter
                EncoderParameter ept = null;
                //EncoderParameters
                EncoderParameters eptS = null;
                try
                {
                    //bmp = new Bitmap(filePath);
                    ici = this.getImageCoderInfo("image/jpeg");
                    ecd = Encoder.Quality;
                    eptS = new EncoderParameters(1);
                    ept = new EncoderParameter(ecd, 10L);
                    eptS.Param[0] = ept;
                   // bm.Save(filePath_ystp, ici, eptS);

                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    bm.Dispose();
                    ept.Dispose();
                    eptS.Dispose();
                }
            }
            return bm;

        }
        private bool ThumbnailCallback()
        {
            return false;
        }

        private ImageCodecInfo getImageCoderInfo(string coderType)// 獲取圖片編碼類型信息
        {
            ImageCodecInfo[] iciS = ImageCodecInfo.GetImageEncoders();

            ImageCodecInfo retIci = null;

            foreach (ImageCodecInfo ici in iciS)
            {
                if (ici.MimeType.Equals(coderType))
                    retIci = ici;
            }

            return retIci;
        }

    }
}

發佈了19 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章