根据文件夹路径,压缩超过指定大小的图片,并上传到指定路径
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace FolderImage { /// <summary> /// 根据文件夹路径,查看下面有哪些图片,如果超过了指定的大小,则压缩图片,上传到指定的路径 /// </summary> public static class ImageFolderHelper { //文件夹下面的所有图片路径 public static List<string> imageList = new List<string>(); /// <summary> /// 获取文件夹下面的文件 /// </summary> public static void ImageFileList(string path = @"E:\测试\inetshop") { DirectoryInfo root = new DirectoryInfo(path); //获取文件夹下面的文件 FileInfo[] files = root.GetFiles(); foreach (FileInfo fi in files) { if (fi.Extension == ".jpg" || fi.Extension == ".gif" || fi.Extension == ".png" || fi.Extension == ".jpeg") { // 写你自己的处理代码 imageList.Add(fi.FullName); ImageByte(fi.FullName); } } //获取文件夹下面的子文件夹 DirectoryInfo[] dics = root.GetDirectories(); foreach (var item in dics) { ImageFileList(item.FullName); } } /// <summary> /// 根据图片大小看是否压缩重新上传 /// </summary> /// <param name="ImageFile">图片路径</param> public static void ImageByte(string ImageFile) { var imagebyte = GetImageByteIam(ImageFile); //读入MemoryStream对象 using (Stream sr = new MemoryStream(imagebyte)) { //超过1M就压缩 if (sr.Length > 1 * 1024 * 1024) { CompressImage(ImageFile, ImageFile.Replace("inetshop", "backshop")); } } } /// <summary> /// 根据本机图片路径返回图片的字节流byte[] /// </summary> /// <param name="imagePath">图片路径</param> public static byte[] GetImageByteIam(string imagePath) { FileStream files = new FileStream(imagePath, FileMode.Open); byte[] imgByte = new byte[files.Length]; files.Read(imgByte, 0, imgByte.Length); files.Close(); return imgByte; } /// <summary> /// 无损压缩图片 /// </summary> /// <param name="sFile">原图片地址</param> /// <param name="dFile">压缩后保存图片地址</param> /// <param name="flag">压缩质量(数字越小压缩率越高)1-100</param> /// <param name="size">压缩后图片的最大大小</param> /// <param name="sfsc">是否是第一次调用</param> /// <returns></returns> public static bool CompressImage(string sFile, string dFile, int flag = 50, int size = 512, bool sfsc = true) { //是否存在文件夹 var index = dFile.LastIndexOf('\\'); var sfilestring = dFile.Substring(0, index); if (!Directory.Exists(sfilestring)) { //创建 DirectoryInfo directoryInfo = new DirectoryInfo(sfilestring); directoryInfo.Create(); } //如果是第一次调用,原始图像的大小小于要压缩的大小,则直接复制文件,并且返回true FileInfo firstFileInfo = new FileInfo(sFile); if (sfsc == true && firstFileInfo.Length < size * 1024) { firstFileInfo.CopyTo(dFile); return true; } System.Drawing.Image iSource = System.Drawing.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 > 1024 * size) { flag = flag - 10; CompressImage(sFile, dFile, flag, size, false); } } else { ob.Save(dFile, tFormat); } return true; } catch (Exception ex) { return false; } finally { iSource.Dispose(); ob.Dispose(); } } } }