批量压缩图片

// See https://aka.ms/new-console-template for more information
using ReduceImage;

Console.WriteLine("Hello, World!");

string folderPath = "C:\\Users\\shiningrise\\Desktop\\fsdownload\\UploadFiles"; // 将此处替换为要遍历的文件夹路径

Bianli(folderPath);

void Bianli(string path)
{
    List<string> temp = new List<string>();
    temp.Add(path);
    for (int i = 0; i < temp.Count; i++)
    {
        if (File.Exists(temp[i]))
        {
            continue;
        }
        else
        {
            List<string> dirs = new List<string>(Directory.EnumerateDirectories(temp[i]));

            foreach (var dir in dirs)
            {
                temp.Add(dir);
            }
            List<string> files = new List<string>(Directory.EnumerateFiles(temp[i]));

            foreach (string file in files)
            {
                temp.Add(file);
            }
        }
    }

    foreach (string pathTemp in temp)
    {
        if (File.Exists(pathTemp))
            Img.ReduceImage(pathTemp, 768, 254);
        Console.WriteLine(pathTemp);
    }
}
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Drawing;

namespace ReduceImage
{
    /// <summary>
    /// 下载远程图片保存到本地地址
    /// </summary>
    public class Img
    {
        public static void ReduceImage(string imageFile, int toWidth, int toHeight)
        {
            try
            {
                Image originalImage = Image.FromFile(imageFile);
                if (toWidth <= 0 && toHeight <= 0)
                {
                    return;
                }
                else if (toWidth > 0 && toHeight > 0)
                {
                    if (originalImage.Width < toWidth && originalImage.Height < toHeight)
                        return;
                }
                else if (toWidth <= 0 && toHeight > 0)
                {
                    if (originalImage.Height < toHeight)
                        return;
                    toWidth = originalImage.Width * toHeight / originalImage.Height;
                }
                else if (toHeight <= 0 && toWidth > 0)
                {
                    if (originalImage.Width < toWidth)
                        return;
                    toHeight = originalImage.Height * toWidth / originalImage.Width;
                }
                Image toBitmap = new Bitmap(toWidth, toHeight);
                try
                {
                    using (Graphics g = Graphics.FromImage(toBitmap))
                    {
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        g.Clear(Color.Transparent);
                        g.DrawImage(originalImage,
                                    new Rectangle(0, 0, toWidth, toHeight),
                                    new Rectangle(0, 0, originalImage.Width, originalImage.Height),
                                    GraphicsUnit.Pixel);
                        originalImage.Dispose();
                        toBitmap.Save(imageFile, System.Drawing.Imaging.ImageFormat.Png);
                        toBitmap.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    if (originalImage != null) originalImage.Dispose();
                    if (toBitmap != null) toBitmap.Dispose();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

 

posted @ 2024-04-10 15:32  shiningrise  阅读(11)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css