今天老大让做一个图片压缩功能,自己在网上找了一些解决方法,也同时对方法进行了提炼!
在下面的代码中会有一个字段Encoder.Quality 字段,这个对应的有个一个values是神奇的值
MSDN解释是:Quality 类别将指定的图像压缩级别。 在用于构造 EncoderParameter, ,质量类别的有用值的范围是从 0 到 100。 指定的数字越,越高压缩和图像质量也因此越低。 零将为您提供最低的图像质量和 100 最高。
当将一个参数传递给图像编码器中时,将参数封装在 EncoderParameter 对象。 域中的一个 EncoderParameter 对象是指定的参数的类别的 GUID。 使用的静态字段 Encoder 类来检索 Encoder ,其中包含参数的所需类别。
内置的图像编码器 GDI+ 接收属于多个类别的参数。 下表列出了所有类别,每个类别关联的 GUID。
里面有6个方法,如下
方法作用 | 方法名 |
---|
按质量比和尺寸收缩呗数,生成缩略图,保存为另外路劲 | getThumImage(String sourceFile, long quality, int multiple, String outputFile) |
按质量比和尺寸收缩呗数,生成缩略图;保存在同一路径 | getThumImage(String sourceFile, long quality, int multiple) |
按图片尺寸大小压缩图片 ,保存在不同路径 | ImageByMultiple(String sourceFile, int multiple, String outputFile) |
按图片尺寸大小压缩图片 ,保存在同一路径 | ImageByMultiple(String sourceFile, int multiple) |
质量压缩比,保存在不同一路径 | ImageByQuality(String sourceFile, int quality, String outputFile) |
按质量压缩比压缩图片,保存在同一路径 | ImageByQuality(String sourceFile, int quality) |
public partial class WebForm2 : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){GetFileInfo(@"D:\tupian");}private void GetFileInfo(string filePath){DirectoryInfo directoryInfo = new DirectoryInfo(filePath);try{foreach (DirectoryInfo childDirectoryInfo in directoryInfo.GetDirectories()){string name = childDirectoryInfo.Name.ToString();GetFileInfo(filePath + "\\" + childDirectoryInfo.Name.ToString());}foreach (FileInfo fileInfo in directoryInfo.GetFiles()){if (fileInfo.Length > 400 * 1024){FixSize(fileInfo.FullName, 200, 100, fileInfo.FullName);}}}catch{}}#region 调用图片处理的方法public static bool getThumImage(String sourceFile, long quality, int multiple, String outputFile){bool flag = false;try{flag = ThumImage(sourceFile, quality, multiple, outputFile);}catch{flag = false;}return flag;}public static bool getThumImage(String sourceFile, long quality, int multiple){bool flag = false;try{flag = ThumImage(sourceFile, quality, multiple, sourceFile);}catch{flag = false;}return flag;}public static bool ImageByMultiple(String sourceFile, int multiple, String outputFile){bool flag = false;try{flag = ThumImageByMultiple(sourceFile, multiple, outputFile);}catch{flag = false;}return flag;}public static bool ImageByMultiple(String sourceFile, int multiple){bool flag = false;try{flag = ThumImageByMultiple(sourceFile, multiple, sourceFile);}catch{flag = false;}return flag;}public static bool ImageByQuality(String sourceFile, int quality, String outputFile){bool flag = false;try{flag = ThumImageByQuality(sourceFile, quality, outputFile);}catch{flag = false;}return flag;}public static bool ImageByQuality(String sourceFile, int quality){bool flag = false;try{flag = ThumImageByQuality(sourceFile, quality, sourceFile);}catch{flag = false;}return flag;}#endregion#region 调用图片处理具体方法private static bool ThumImageByQuality(String sourceFile, long quality, String outputFile){bool flag = false;try{long imageQuality = quality;Bitmap sourceImage = new Bitmap(sourceFile);ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;EncoderParameters myEncoderParameters = new EncoderParameters(1);EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality);myEncoderParameters.Param[0] = myEncoderParameter;float xWidth = sourceImage.Width;float yWidth = sourceImage.Height;Bitmap newImage = new Bitmap((int)(xWidth), (int)(yWidth));Graphics g = Graphics.FromImage(newImage);g.DrawImage(sourceImage, 0, 0, xWidth, yWidth);sourceImage.Dispose();g.Dispose();newImage.Save(outputFile, myImageCodecInfo, myEncoderParameters);flag = true;}catch{flag = false;}return flag;}private static bool ThumImage(String sourceFile, long quality, int multiple, String outputFile){bool flag = false;try{long imageQuality = quality;Bitmap sourceImage = new Bitmap(sourceFile);ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;EncoderParameters myEncoderParameters = new EncoderParameters(1);EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality);myEncoderParameters.Param[0] = myEncoderParameter;float xWidth = sourceImage.Width;float yWidth = sourceImage.Height;Bitmap newImage = new Bitmap((int)(xWidth / multiple), (int)(yWidth / multiple));Graphics g = Graphics.FromImage(newImage);g.DrawImage(sourceImage, 0, 0, xWidth / multiple, yWidth / multiple);sourceImage.Dispose();g.Dispose();newImage.Save(outputFile, myImageCodecInfo, myEncoderParameters);flag = true;}catch{flag = false;}return flag;}public static bool ThumImageByMultiple(String sourceFile, int multiple, String outputFile){try{Bitmap sourceImage = new Bitmap(sourceFile);ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");float xWidth = sourceImage.Width;float yWidth = sourceImage.Height;Bitmap newImage = new Bitmap((int)(xWidth / multiple), (int)(yWidth / multiple));Graphics g = Graphics.FromImage(newImage);g.DrawImage(sourceImage, 0, 0, xWidth / multiple, yWidth / multiple);sourceImage.Dispose();g.Dispose();newImage.Save(outputFile);return true;}catch{return false;}}public static bool FixSize(String sourceFile, int xWidth, int yWidth, String outputFile){try{Bitmap sourceImage = new Bitmap(sourceFile);ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");Bitmap newImage = new Bitmap((int)(xWidth), (int)(yWidth));Graphics g = Graphics.FromImage(newImage);g.DrawImage(sourceImage, 0, 0, xWidth, yWidth);sourceImage.Dispose();g.Dispose();newImage.Save(outputFile);return true;}catch{return false;}}#endregion getThumImageprivate static ImageCodecInfo GetEncoderInfo(String mimeType){int j;ImageCodecInfo[] encoders;encoders = ImageCodecInfo.GetImageEncoders();for (j = 0; j < encoders.Length; ++j){if (encoders[j].MimeType == mimeType)return encoders[j];}return null;}}