C#图片压缩、生成缩略图、添加水印模块
2014-08-20来源:易贤网

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}

注:更多精彩文章请关注三联编程教程栏目。

更多信息请查看IT技术专栏

推荐信息