批量压缩图片

简介: 批量压缩图片
// 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);
            }
        }
    }
}
目录
相关文章
|
6月前
|
Ubuntu Linux 网络安全
防火墙设置
本文介绍了Ubuntu和CentOS系统中防火墙的基本设置方法。对于Ubuntu,重点讲解了UFW(Uncomplicated Firewall)的使用,包括查看状态、开启/关闭防火墙、管理端口和IP地址以及服务配置。而对于CentOS,主要涉及firewalld的常用操作,如查看状态、启动/关闭防火墙、设置开机启动、管理端口和IP地址,以及服务允许等具体命令示例。内容简洁实用,适合系统管理员快速上手。
480 10
|
10月前
|
Windows
写一个批处理,压缩一个文件夹下的所有图片大小
【10月更文挑战第14天】在Windows环境下,使用批处理脚本可以方便地批量压缩图片。以下是一个示例脚本,用于压缩指定目录下的所有.jpg和.png文件,并将压缩后的图片保存到另一个目录中。
606 3
|
10月前
|
API
图片压缩+格式转换免费API接口教程
这是一个免费的图片压缩和格式转换API接口,支持GET和POST请求。请求地址为 `https://cn.apihz.cn/api/img/yasuo.php`,需提供 `id`、`key`、`img` 等参数。返回数据包含处理后的图片URL和其他相关信息。更多详情请参考:https://www.apihz.cn/api/imgyasuo.html
|
10月前
|
负载均衡 监控 API
dotnet微服务之API网关Ocelot
Ocelot 是一个基于 .NET 的 API 网关,适用于微服务架构。本文介绍了如何创建一个 Web API 项目并使用 Ocelot 进行 API 请求路由、负载均衡等。通过配置 `ocelot.json` 和修改 `Program.cs`,实现对 `GoodApi` 和 `OrderApi` 两个项目的路由管理。最终,通过访问 `https://localhost:7122/good/Hello` 和 `https://localhost:7122/order/Hello` 验证配置成功。
176 1
dotnet微服务之API网关Ocelot
|
10月前
|
开发框架 前端开发 .NET
Abp源码分析之Serilog日志
本文介绍了如何在ASP.NET Core MVC项目和ABP框架中配置和使用Serilog日志库。通过修改`Program.cs`文件,配置日志级别、输出目标,并在控制器和页面模型中记录日志。具体步骤包括新建MVC项目、配置日志、修改控制器和首页代码。最终,日志将被记录到控制台和`Logs/logs.txt`文件中。
161 1
Abp源码分析之Serilog日志
|
10月前
|
API Docker 微服务
Ocelot集成Consul实现api网关与服务发现
本文介绍了如何在.NET微服务架构中集成API网关Ocelot和Consul服务发现。首先通过Docker安装并配置Consul,接着在GoodApi项目中实现服务的自动注册与注销,并配置健康检查。然后,通过修改Ocelot的配置文件`ocelot.json`和`Program.cs`,实现基于Consul的服务发现,确保API请求能够正确路由到后端服务。最后,解决了服务解析时可能出现的问题,确保服务的IP地址而非节点名称被正确解析。
228 0
Ocelot集成Consul实现api网关与服务发现
|
10月前
|
存储 开发框架 前端开发
Abp源码分析之虚拟文件系统Volo.Abp.VirtualFileSystem
`Volo.Abp.VirtualFileSystem` 是 ABP 框架中的一个重要组件,提供了一种抽象文件系统的方式,使应用程序可以轻松访问和管理文件资源。本文介绍了如何在 MVC 项目中使用 `Volo.Abp.VirtualFileSystem`,包括新建项目、配置模块、添加资源文件以及读取资源文件的具体步骤。通过统一的接口处理文件和目录,无论实际存储位置如何,应用程序都能更加灵活地切换不同的文件存储方式。
146 0
Abp源码分析之虚拟文件系统Volo.Abp.VirtualFileSystem
|
10月前
|
JSON 开发框架 自然语言处理
Abp源码分析之Abp本地化
本文介绍了如何在 ASP.NET Core MVC 项目中实现本地化功能,包括使用资源文件和 JSON 文件两种方式。首先,通过修改 `Program.cs` 配置支持的多语言环境,并创建相应的资源文件目录。接着,展示了如何在视图中使用本地化字符串。此外,还介绍了使用 ABP 框架实现本地化的具体步骤,包括新建模块、配置服务和创建资源文件。最后,通过源码分析详细解释了本地化机制的实现原理。
206 0
Abp源码分析之Abp本地化
|
11月前
|
数据安全/隐私保护
sm4加密工具类
sm4加密工具类
145 4
|
11月前
|
数据安全/隐私保护
sm2加密解密
sm2加密解密
144 3