.NET 等寬、等高、等比例、固定寬高產生縮圖 類

來源:互聯網
上載者:User

標籤:system   pat   reg   form   stat   模式   高品質   from   背景色   

根據原圖片產生等比縮圖#region 根據原圖片產生等比縮圖        /// <summary>          /// 根據源圖片產生縮圖          /// </summary>          /// <param name="imgPath_old">源圖(大圖)實體路徑</param>          /// <param name="imgPath_new">縮圖實體路徑(產生的縮圖將儲存到該物理位置)</param>          /// <param name="width">縮圖寬度</param>          /// <param name="height">縮圖高度</param>          /// <param name="mode">縮圖縮放模式(取值"HW":指定高寬縮放,可能變形;取值"W":按指定寬度,高度按比例縮放;取值"H":按指定高度,寬度按比例縮放;取值"Cut":按指定高度和寬度裁剪,不變形);取值"DB":等比縮放,以值較大的作為標準進行等比縮放</param>          /// <param name="type">即將產生縮圖的檔案的副檔名(僅限:JPG、GIF、PNG、BMP)</param>          public static void MakeThumbnail(string imgPath_old, string imgPath_new, int width, int height, string mode, string imageType, int xx, int yy)        {            System.Drawing.Image img = System.Drawing.Image.FromFile(imgPath_old);            int towidth = width; int toheight = height;            int x = 0; int y = 0; int ow = img.Width;            int oh = img.Height; switch (mode)            {                case "HW":  //指定高寬壓縮                    if ((double)img.Width / (double)img.Height > (double)width / (double)height)//判斷圖形是什麼形狀                    {                        towidth = width;                        toheight = img.Height * width / img.Width;                    }                    else if ((double)img.Width / (double)img.Height == (double)width / (double)height)                    {                        towidth = width;                        toheight = height;                    }                    else                    {                        toheight = height;                        towidth = img.Width * height / img.Height;                    }                    break;                case "W":  //指定寬,高按比例                       toheight = img.Height * width / img.Width;                    break;                case "H":  //指定高,寬按比例                      towidth = img.Width * height / 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 = yy; x = (img.Width - ow) / 2;                    }                    else                    {                        ow = img.Width;                        oh = img.Width * height / towidth;                        x = xx; y = (img.Height - oh) / 2;                    } break;                case "DB":    // 按值較大的進行等比縮放(不變形)                       if ((double)img.Width / (double)towidth < (double)img.Height / (double)toheight)                    {                        toheight = height;                        towidth = img.Width * height / img.Height;                    }                    else                    {                        towidth = width;                        toheight = img.Height * width / img.Width;                    } break;                default:                    break;            }            //建立一個bmp圖片              System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);            //建立一個畫板              System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);            //設定高品質插值法              g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;            //設定高品質,低速度呈現平滑程度              g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;            //清空畫布並以透明背景色填充              g.Clear(System.Drawing.Color.Transparent);            //在指定位置並且按指定大小繪製原圖片的指定部分              g.DrawImage(img, new System.Drawing.Rectangle(0, 0, towidth, toheight),            new System.Drawing.Rectangle(x, y, ow, oh),            System.Drawing.GraphicsUnit.Pixel); try            {                //以jpg格式儲存縮圖                 switch (imageType.ToLower())                {                    case "gif":                        img.Save(imgPath_new, ImageFormat.Jpeg);//產生縮圖                        break;                    case "jpg":                        bitmap.Save(imgPath_new, System.Drawing.Imaging.ImageFormat.Jpeg);                        break;                    case "bmp":                        bitmap.Save(imgPath_new, System.Drawing.Imaging.ImageFormat.Bmp);                        break;                    case "png":                        bitmap.Save(imgPath_new, System.Drawing.Imaging.ImageFormat.Png);                        break;                    default:                        bitmap.Save(imgPath_new, System.Drawing.Imaging.ImageFormat.Jpeg);                        break;                }                ////儲存縮圖                  // bitmap.Save(imgPath_new);            }            catch (System.Exception e)            {                throw e;            }            finally            {                img.Dispose();                bitmap.Dispose(); g.Dispose();            }        }        #endregion

 

.NET 等寬、等高、等比例、固定寬高產生縮圖 類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.