C#根據大圖片產生高清縮圖

來源:互聯網
上載者:User

   C#產生高清縮圖代碼,一個C#函數模組,內含注釋,後附函數參數,下面來看這個C#產生縮圖代碼:

  01public static void SetGoodImage(string fileName, string newFile, int maxHeight, int maxWidth,long qualitys)

  02{

  03 if (qualitys == 0)

  04 {

  05 qualitys = 80;

  06 }

  07 using (System.Drawing.Image img = System.Drawing.Image.FromFile(fileName))

  08 {

  09 System.Drawing.Imaging.ImageFormat

  10 thisFormat = img.RawFormat;

  11 Size newSize = NewSize(maxWidth, maxHeight, img.Width, img.Height);

  12 Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);

  13 Graphics g = Graphics.FromImage(outBmp);

  14 // 設定畫布的描繪品質

  15 g.CompositingQuality = CompositingQuality.HighQuality;

  16 g.SmoothingMode = SmoothingMode.HighQuality;

  17 g.InterpolationMode = InterpolationMode.HighQualityBicubic;

  18 g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height),

  19 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);

  20 g.Dispose();

  21 // 以下代碼為儲存圖片時,設定壓縮品質

  22 EncoderParameters encoderParams = new EncoderParameters();

  23 long[] quality = new long[1];

  24 quality[0] = qualitys;

  25 EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

  26 encoderParams.Param[0] = encoderParam;

  27 //獲得包含有關內建映像編碼解碼器的資訊的ImageCodecInfo 對象.

  28 ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();

  29 ImageCodecInfo jpegICI = null;

  30 for (int x = 0;

  31 x < arrayICI.Length;

  32 x++)

  33 {

  34 if (arrayICI[x].FormatDescription.Equals("JPEG"))

  35 {

  36 jpegICI = arrayICI[x];

  37 //設定JPEG編碼

  38 break;

  39 }

  40 }

  41 if (jpegICI != null)

  42 {

  43 outBmp.Save(newFile, jpegICI, encoderParams);

  44 }

  45 else

  46 {

  47 outBmp.Save(newFile, thisFormat);

  48 }

  49 img.Dispose();

  50 outBmp.Dispose();

  51 }

  52}

  函數參數說明:

  view sourceprint?1/// 原檔案

  2/// 新檔案

  3/// 最大高度

  4/// 最大寬度

  5/// 品質,如果為0,則設為80

        :更多精彩文章請關注三聯編程教程欄目。

聯繫我們

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