自动裁剪图片
自动裁剪商品图片
View Code
执行裁剪指定目录商品图片动作
///<summary> ///执行指定目录商品图片动作 ///</summary> public static void FindPictureDoCutIt(object o) { string filePath=o.ToString(); try { DirectioryInfo fatherFolder = new DirectioryInfo(filePath); //删除当前文件夹内文件 FileInfo[] files = fatherFolder.GetFiles(); foreach(FileInfo file in files) { string fileName=fatherFolder.GetFiles(); try { string spufilename = file.Name.Substring(0,file.Name.IndexOf('.')); string spuhouzhui=Path.GetExtension(file.FullName); #region 检测是不是主图片 string[] mainSpu=spufilename.Split('_'); if(mainSpu.Length>2) { //如果是例似 _宽_高 则不是主图,不进行处理 continue; } #endregion #对文件进行剪裁成不同的尺寸 Spu图片剪裁 Directionary<int,int> ImageSizelist=XmlHelper.GetImageSize("SouImage");//取得要剪裁的图片尺寸 Bitmap bitmap=new Bitmap(file.FullName);//原始图片 foreach(var image in ImageSizelist) { Bitmap spuimage = ImageHelper.GetThumbnail(bitmap,image.Key,image.Value); string saveSpuName=filePath+"\\"+spufilename+"_"+image.Key+"_"+image.Value+"_"+spiouzhui; spuimage.Save(saveSpuName);//保存裁剪后的图片 spuimage.Dispose(); } bitmap.Dispose(); #endregion } catch(Exception ex) {} } //递归查找子文件夹内文件 foreach(DirectoryInfo chileFolder in fatherFolde.GetDirectories()) { FindPricureDoCutIt(childFolder.FullName); } } finally { GC.Collect(); } }
不忘初心,方得始终