C# 圖片裁剪代碼

來源:互聯網
上載者:User

標籤:style   blog   color   ar   for   div   代碼   log   sp   

        /// <summary>        /// 縮小裁剪圖片        /// </summary>        /// <param name="int_Width">要縮小裁剪圖片寬度</param>        /// <param name="int_Height">要縮小裁剪圖片長度</param>        /// <param name="input_ImgUrl">要處理圖片路徑</param>        /// <param name="out_ImgUrl">處理完畢圖片路徑</param>        public void ImgReduceCutOut(int int_Width, int int_Height, string input_ImgUrl, string out_ImgUrl)        {            // ===上傳標準圖大小===            int int_Standard_Width = 160;            int int_Standard_Height = 160;            int Reduce_Width = 0; // 縮小的寬度            int Reduce_Height = 0; // 縮小的高度            int CutOut_Width = 0; // 裁剪的寬度            int CutOut_Height = 0; // 裁剪的高度            int level = 100; //縮圖的品質 1-100的範圍            // ===獲得縮小,裁剪大小===            if (int_Standard_Height * int_Width / int_Standard_Width > int_Height)            {                Reduce_Width = int_Width;                Reduce_Height = int_Standard_Height * int_Width / int_Standard_Width;                CutOut_Width = int_Width;                CutOut_Height = int_Height;            }            else if (int_Standard_Height * int_Width / int_Standard_Width < int_Height)            {                Reduce_Width = int_Standard_Width * int_Height / int_Standard_Height;                Reduce_Height = int_Height;                CutOut_Width = int_Width;                CutOut_Height = int_Height;            }            else            {                Reduce_Width = int_Width;                Reduce_Height = int_Height;                CutOut_Width = int_Width;                CutOut_Height = int_Height;            }            // ===通過串連建立Image對象===            System.Drawing.Image oldimage = System.Drawing.Image.FromFile(Server.MapPath(input_ImgUrl));            // ===縮小圖片===            System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage(Reduce_Width, Reduce_Height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);            Bitmap bm = new Bitmap(thumbnailImage);            // ===處理JPG品質的函數===            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();            ImageCodecInfo ici = null;            foreach (ImageCodecInfo codec in codecs)            {                if (codec.MimeType == "image/jpeg")                    ici = codec;            }            EncoderParameters ep = new EncoderParameters();            ep.Param[0] = new EncoderParameter(Encoder.Quality, (long)level);            //bm.Save(Server.MapPath("2.jpg"), ici, ep);            // ===裁剪圖片===            Rectangle cloneRect = new Rectangle(0, 0, CutOut_Width, CutOut_Height);            PixelFormat format = bm.PixelFormat;            Bitmap cloneBitmap = bm.Clone(cloneRect, format);            // ===儲存圖片===            cloneBitmap.Save(Server.MapPath(out_ImgUrl), ici, ep);        }

 

C# 圖片裁剪代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.