C# 修改图片文件分辨率大小

将文件夹及子文件夹下的所有.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();
        }
    原文作者:lwwl12
    原文地址: https://blog.csdn.net/lwwl12/article/details/78963163
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞