天天看點

圖檔等比例縮放

 由于最近開發的項目中需要對上傳的大圖檔做裁剪、縮放處理。是以整理了一下。特此記錄。

        方法: 

      public class ImageHandler

<a></a>

    {

        /// &lt;summary&gt;

        /// 對上傳的圖檔進行等比縮放

        /// &lt;/summary&gt;

        /// http://www.cnblogs.com/babycool

        /// &lt;param name="fromFile"&gt;擷取檔案流Stream&lt;/param&gt;

        /// &lt;param name="fileSaveUrl"&gt;縮略圖儲存完整路徑&lt;/param&gt;

        /// &lt;param name="targetWidth"&gt;模闆寬度&lt;/param&gt;

        /// &lt;param name="targetHeight"&gt;模闆高度&lt;/param&gt;

        public static void ZoomPic(System.IO.Stream fromFile, string fileSaveUrl, System.Double targetWidth, System.Double targetHeight)

        {

            //原始圖檔(擷取原始圖檔建立對象,并使用流中嵌入的顔色管理資訊)

            System.Drawing.Image initImage = System.Drawing.Image.FromStream(fromFile, true);

            //原圖寬高均小于模版,不作處理,直接儲存

            if (initImage.Width &lt;= targetWidth &amp;&amp; initImage.Height &lt;= targetHeight)

            {

                //儲存

                initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);

            }

            else

                //縮略圖寬、高計算

                double newWidth = initImage.Width;

                double newHeight = initImage.Height;

              //寬大于高或寬等于高(橫圖或正方)

                if (initImage.Width &gt; initImage.Height || initImage.Width == initImage.Height)

                {

                    //如果寬大于模版

                    if (initImage.Width &gt; targetWidth)

                    {

                        //寬按模版,高按比例縮放

                        newWidth = targetWidth;

                        newHeight = initImage.Height * (targetWidth / initImage.Width);

                    }

                }

                //高大于寬(豎圖)

                else

                    //如果高大于模版

                    if (initImage.Height &gt; targetHeight)

                        //高按模版,寬按比例縮放

                        newHeight = targetHeight;

                        newWidth = initImage.Width * (targetHeight / initImage.Height);

                //生成新圖

                //建立一個bmp圖檔

                System.Drawing.Image newImage = new System.Drawing.Bitmap((int)newWidth, (int)newHeight);

                //建立一個畫闆

                System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage);

                //設定品質

                newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                //置背景色

                newG.Clear(Color.White);

                //畫圖

                newG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, newImage.Width, newImage.Height), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel);

                //儲存縮略圖

                newImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);

                //釋放資源

                newG.Dispose();

                newImage.Dispose();

                initImage.Dispose();

        }

    }

 調用:

        //接收上傳後的檔案

       HttpPostedFile file = context.Request.Files["Filedata"];

        //處理圖檔

       ImageHandler.ZoomPic(file.InputStream, uploadPath + file.FileName, 435,600);

注意:ZoomPic方法中的第二個參數“fileSaveUrl”是縮略圖儲存的完整路徑如“uploadPath\123.jpg”的形式,如果隻有目錄路徑則會報“GDI+中發生一般性錯誤。”的錯誤提示。是以這裡一定要注意寫全。

 相關文章參考:

 原創文章,轉載請注明出處。

本文轉自 酷小孩 部落格園部落格,原文連結:http://www.cnblogs.com/babycool/archive/2012/11/18/2775916.html  ,如需轉載請自行聯系原作者