C#实现图片压缩
using System; using System.Collections.Generic; using System.Configuration; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Console.WriteLine("开始压缩"); var DefinitionPath = ConfigurationManager.AppSettings["DefinitionPath"];//高清图片保存的项目相对路径 var ThumbnailPath = ConfigurationManager.AppSettings["ThumbnailPath"];//项目保存相对路径 var file = Directory.GetFiles(DefinitionPath, "*.jpg"); var file1 = Directory.GetFiles(DefinitionPath, "*.png"); if (!System.IO.Directory.Exists(ThumbnailPath)) System.IO.Directory.CreateDirectory(ThumbnailPath); #region 压缩图片 foreach (var item in file) { string file2 = item.Substring(DefinitionPath.Length + 1); //string file = item.Substring(DefinitionPath.Length); Console.WriteLine(file2); CompressImage(item, ThumbnailPath + @"\" + file2, 100, 300, true); } foreach (var item in file1) { string file2 = item.Substring(DefinitionPath.Length + 1); //string file = item.Substring(DefinitionPath.Length); CompressImage(item, ThumbnailPath + @"\" + file2, 90, 300, true); } #endregion #region 不执行 //var file = Directory.GetFiles(ThumbnailPath, "*.jpg"); //var file1 = Directory.GetFiles(ThumbnailPath, "*.png"); //foreach (var item in file) //{ // string file2 = item.Substring(ThumbnailPath.Length + 1); // //string file = item.Substring(DefinitionPath.Length); // FileInfo fileInfo = new FileInfo(item); // if (fileInfo.Length > 40960) // { // CompressImage(item, ThumbnailPath1 + @"\" + file2, 90, 300, true); // } //} //foreach (var item in file1) //{ // string file2 = item.Substring(ThumbnailPath.Length + 1); // //string file = item.Substring(DefinitionPath.Length); // FileInfo fileInfo = new FileInfo(item); // if (fileInfo.Length > 40960) // { // CompressImage(item, ThumbnailPath1 + @"\" + file2, 90, 300, true); // } //} #endregion Console.WriteLine("压缩完成"); Console.ReadKey(); } /// <summary> /// /// </summary> /// <param name="sFile">图片原本路径</param> /// <param name="dFile">压缩后的路径</param> /// <param name="flag"></param> /// <param name="size"></param> /// <param name="sfsc"></param> /// <returns></returns> public static bool CompressImage(string sFile, string dFile, int flag = 90, int size = 300, bool sfsc = true) { if (sfsc) { FileInfo firstFileInfo1 = new FileInfo(sFile); firstFileInfo1.CopyTo(dFile,true); } //如果是第一次调用,原始图像的大小小于要压缩的大小,则直接复制文件,并且返回true FileInfo firstFileInfo = new FileInfo(dFile); //string a = firstFileInfo.Length if (sfsc == true && firstFileInfo.Length < 40960 || flag == 0) { //firstFileInfo.CopyTo(dFile, true); return true; } Image iSource = Image.FromFile(sFile); ImageFormat tFormat = iSource.RawFormat; int dHeight = iSource.Height / 2; int dWidth = iSource.Width / 2; int sW = 0, sH = 0; //按比例缩放 Size tem_size = new Size(iSource.Width, iSource.Height); if (tem_size.Width > dHeight || tem_size.Width > dWidth) { if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth)) { sW = dWidth; sH = (dWidth * tem_size.Height) / tem_size.Width; } else { sH = dHeight; sW = (tem_size.Width * dHeight) / tem_size.Height; } } else { sW = tem_size.Width; sH = tem_size.Height; } Bitmap ob = new Bitmap(dWidth, dHeight); Graphics g = Graphics.FromImage(ob); g.Clear(Color.WhiteSmoke); g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel); g.Dispose(); //以下代码为保存图片时,设置压缩质量 EncoderParameters ep = new EncoderParameters(); long[] qy = new long[1]; qy[0] = flag;//设置压缩的比例1-100 EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy); ep.Param[0] = eParam; try { ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegICIinfo = null; for (int x = 0; x < arrayICI.Length; x++) { if (arrayICI[x].FormatDescription.Equals("JPEG")) { jpegICIinfo = arrayICI[x]; break; } } if (jpegICIinfo != null) { ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径 FileInfo fi = new FileInfo(dFile); if (fi.Length > 40960) { flag = flag - 5; if (flag >= 0) { CompressImage(sFile, dFile, flag, size, false); } else { ob.Save(dFile, tFormat); Console.WriteLine(dFile); } } } else { ob.Save(dFile, tFormat); Console.WriteLine(dFile); } return true; } catch (Exception ex) { File.WriteAllText(@"D:\\Imgload.TXT", ex.ToString()); return false; } finally { iSource.Dispose(); ob.Dispose(); } } } }
这里使用的是画图,并非文件流