public CaseModel Func_Compression() { return(new CaseModel() { NameSign = @"压缩计算", ExeEvent = () => { const string oldpath = @"D:\ZRQDownloads\imgs"; if (!Directory.Exists(oldpath)) { Console.WriteLine("停止执行, 路径不存在: {0}", oldpath); return true; } ImageHandler handler = new ImageHandler(); const int maxwidth = 1200; const int maxheight = 1200; handler.SetMaxSize(maxwidth, maxheight); const long maxbytelength = 2 * 1024 * 1024; handler.SetMaxByteLength(maxbytelength); DirectoryInfo[] sondirs = PathHelp.AllSonDirectorys(new DirectoryInfo(oldpath)); foreach (DirectoryInfo dirinfo in sondirs) { FileInfo[] fis = PathHelp.PatternFileInfo(dirinfo, @".*\.(jpg|png|gif)"); foreach (FileInfo file in fis) { byte[] imgvalues = handler.Calc(file); if (CheckData.IsSizeEmpty(imgvalues)) { Console.WriteLine("压缩图片失败, 结果为空!"); Console.WriteLine("FileInfo.FullName: {0}", file.FullName); return false; } string newdirpath = file.DirectoryName.Replace("imgs", "imgs_c"); string newfilepath = PathHelp.CreateUseFilePath(newdirpath, file.Name); handler.SaveImg(imgvalues, newfilepath); } } return true; }, }); }