将文件夹及子文件夹下的所有.jpg格式的图片宽度调整成统一大小,并包存到指定文件夹内。
public static void ResizeImg(string srcFold, string desFold, int maxSize)
{
var ret = Directory.GetFiles(srcFold, "*.jpg");
foreach (var v in ret)
{
Image_Resize(v, desFold, maxSize);
}
var folds = Directory.GetDirectories(srcFold);
foreach (var v in folds)
{
ResizeImg(v, desFold, maxSize);
}
}
public static void Image_Resize(string srcPath, string desFold, int maxSize)
{
//string currentPath = srcPath.Substring(0, srcPath.LastIndexOf("\\"));
string currentPath = desFold;
string fileName = srcPath.Substring(srcPath.LastIndexOf("\\") + 1, srcPath.Length - srcPath.LastIndexOf("\\") - 1);
string newFileName = "new" + fileName;
Image img = Image.FromFile(srcPath);
int width = maxSize;
Image newImg = new Bitmap(width, img.Height * width / img.Width);
Graphics g = Graphics.FromImage(newImg);
g.DrawImage(img, 0, 0, width, img.Height * maxSize / img.Width);
newImg.Save(currentPath + newFileName);
img.Dispose();
newImg.Dispose();
}