萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> C#圖片壓縮、生成縮略圖、添加水印模塊

C#圖片壓縮、生成縮略圖、添加水印模塊

   C#圖片處理工具類,可以壓縮JPG圖像,圖片自動生成縮略圖,為圖片添加水印效果等,返回高清縮略圖,得到最佳的圖片比例縮放尺寸,並獲取圖片類型等,類代碼如下:

  view sourceprint?001using System;

  002using System.Collections.Generic;

  003using System.Linq;

  004using System.Text;

  005using System.IO;

  006using System.Drawing;

  007using System.Drawing.Drawing2D;

  008using System.Drawing.Imaging;

  009namespace CLB.Utility.Tools

  010{

  011 ///

  012 /// 圖片工具類

  013 ///

  014 public static class ImageHelper

  015 {

  016 ///

  017 /// 壓縮JPG圖片

  018 ///

  019 /// 壓縮後圖片存放的地址

  020 /// 需要壓縮的圖片地址

  021 /// 壓縮質量:如果為0則默認調整為80

  022 public static void SetCompressImage(string NewfileName, string OldfileName, long quality)

  023 {

  024 if (quality == 0)

  025 {

  026 quality = 80;

  027 }

  028 using (Bitmap bitmp = new Bitmap(OldfileName))

  029 {

  030 EncoderParameters ep = new EncoderParameters(1);

  031 ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

  032 ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");

  033 bitmp.Save(NewfileName, myImageCodecInfo, ep);

  034 bitmp.Dispose();

  035 }

  036 }

  037 ///

  038 /// 返回高清縮略圖

  039 ///

  040 /// 原文件

  041 /// 新文件

  042 /// 最大高度

  043 /// 最大寬度

  044 /// 質量,如果為0,則設為80

  045 public static void SetGoodImage(string fileName, string newFile, int maxHeight, int maxWidth,longqualitys)

  046 {

  047 if (qualitys == 0)

  048 {

  049 qualitys = 80;

  050 }

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

  052 {

  053 System.Drawing.Imaging.ImageFormat

  054 thisFormat = img.RawFormat;

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

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

  057 Graphics g = Graphics.FromImage(outBmp);

  058 // 設置畫布的描繪質量

  059 g.CompositingQuality = CompositingQuality.HighQuality;

  060 g.SmoothingMode = SmoothingMode.HighQuality;

  061 g.InterpolationMode = InterpolationMode.HighQualityBicubic;

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

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

  064 g.Dispose();

  065 // 以下代碼為保存圖片時,設置壓縮質量

  066 EncoderParameters encoderParams = new EncoderParameters();

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

  068 quality[0] = qualitys;

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

  070 encoderParams.Param[0] = encoderParam;

  071 //獲得包含有關內置圖像編碼解碼器的信息的ImageCodecInfo 對象.

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

  073 ImageCodecInfo jpegICI = null;

  074 for (int x = 0;

  075 x < arrayICI.Length;

  076 x++)

  077 {

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

  079 {

  080 jpegICI = arrayICI[x];

  081 //設置JPEG編碼

  082 break;

  083 }

  084 }

  085 if (jpegICI != null)

  086 {

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

  088 }

  089 else

  090 {

  091 outBmp.Save(newFile, thisFormat);

  092 }

  093 img.Dispose();

  094 outBmp.Dispose();

  095 }

  096 }

  097 // 得到到按比例最佳尺寸

  098 private static Size NewSize(int maxWidth, int maxHeight, int width, int height)

  099 {

  100 double w = 0.0;

  101 double h = 0.0;

  102 double sw = Convert.ToDouble(width);

  103 double sh = Convert.ToDouble(height);

  104 double mw = Convert.ToDouble(maxWidth);

  105 double mh = Convert.ToDouble(maxHeight);

  106 if (sw < mw && sh < mh)

  107 {

  108 w = sw;

  109 h = sh;

  110 }

  111 else if ((sw / sh) > (mw / mh))

  112 {

  113 w = maxWidth;

  114 h = (w * sh) / sw;

  115 }

  116 else

  117 {

  118 h = maxHeight;

  119 w = (h * sw) / sh;

  120 }

  121 return new Size(Convert.ToInt32(w), Convert.ToInt32(h));

  122 }

  123 ///

  124 /// 得到圖片類型

  125 ///

  126 ///

  127 ///

  128 private static ImageCodecInfo GetEncoderInfo(String mimeType)

  129 {

  130 int j;

  131 ImageCodecInfo[] encoders;

  132 encoders = ImageCodecInfo.GetImageEncoders();

  133 for (j = 0; j < encoders.Length; ++j)

  134 {

  135 if (encoders[j].MimeType == mimeType)

  136 return encoders[j];

  137 }

  138 return null;

  139 }

  140 ///

  141 /// 添加水印效果

  142 ///

  143 /// 輸入路徑

  144 /// 輸出路徑

  145 /// 水印文件路徑

  146 /// 水印靠近圖片右邊的像素

  147 /// 水印靠近底邊的像素

  148 /// 透明度

  149 public static void SetWaterMark( string fileName, string newfileName,string WaterImg,int RightSpace,intBottomSpace,int LucencyPercent)

  150 {

  151 using (System.Drawing.Image image = System.Drawing.Image.FromFile(fileName))

  152 {

  153 ImageModification wm = new ImageModification();

  154 wm.DrawedImagePath = WaterImg; //水印圖片

  155 wm.ModifyImagePath = fileName; //圖片的路徑

  156 wm.RightSpace = RightSpace; //水印位置

  157 wm.BottoamSpace = image.Height - BottomSpace; //水銀位置

  158 wm.LucencyPercent = LucencyPercent; //透明度

  159 wm.OutPath = newfileName; //生成的文件名

  160 wm.DrawImage();

  161 image.Dispose();

  162 }

  163 }

  164 }

  165}

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

copyright © 萬盛學電腦網 all rights reserved