项目需要,经常需要手动压缩图片,流程太过麻烦,效率低下。所以写了一个小程序,以提高工作效率
using System;
using System.Net;
using System.Text;
using System.IO;
class Program
{
static void Main()
{
Console.WriteLine("请输入TinyPng.com的API KEY,获取地址:https://tinypng.com/developers");
string key = Console.ReadLine();
if (!System.IO.Directory.Exists("NewImg"))
{
System.IO.Directory.CreateDirectory("NewImg");
Console.WriteLine("已创建NewImg目录,请把需要处理的图片放到该目录下");
Console.ReadKey(true);
return;
}
if (!System.IO.Directory.Exists("CompressedImg")){
System.IO.Directory.CreateDirectory("CompressedImg");
}
string url = "https://api.tinify.com/shrink";
string[] fileStrs = System.IO.Directory.GetFiles("NewImg");
Console.WriteLine("共" + fileStrs.Length + "个文件,请等待下载完成...");
int num = 0;
foreach (string s in fileStrs)
{
System.IO.FileInfo info = new System.IO.FileInfo(s);
if (info.Extension == ".png" || info.Extension == ".jpg")
{
string input = @"NewImg/" + info.Name;
string output = @"CompressedImg/" + info.Name;
GetCompressImg(url, key, input, output);
num++;
}
}
Console.WriteLine("文件已下载到CompressedImg文件夹下,共" + num + "个文件");
Console.ReadKey(true);
}
static public void GetCompressImg(string url,string key,string input,string output){
WebClient client = new WebClient();
string auth = Convert.ToBase64String(Encoding.UTF8.GetBytes("api:" + key));
client.Headers.Add(HttpRequestHeader.Authorization, "Basic " + auth);
try
{
client.UploadData(url, File.ReadAllBytes(input));
client.DownloadFile(client.ResponseHeaders["Location"], output);
}
catch (WebException)
{
Console.WriteLine("网络请求失败:" + input);
}
}
}
上面的程序在下载期间会阻塞主线程,效率不够高。
使用await/async改写成异步模式。
using System;
using System.Net;
using System.Text;
using System.IO;
using System.Threading.Tasks;
class Program
{
static public int fileNums = 0;
static public int status = 0;
static void Main()
{
Console.WriteLine("请输入TinyPng.com的API KEY,获取地址:https://tinypng.com/developers");
string key = Console.ReadLine();
if (!System.IO.Directory.Exists("NewImg"))
{
System.IO.Directory.CreateDirectory("NewImg");
Console.WriteLine("已创建NewImg目录,请把需要处理的图片放到该目录下");
Console.ReadKey(true);
return;
}
if (!System.IO.Directory.Exists("CompressedImg")){
System.IO.Directory.CreateDirectory("CompressedImg");
}
string url = "https://api.tinify.com/shrink";
string[] fileStrs = System.IO.Directory.GetFiles("NewImg");
fileNums = fileStrs.Length;
foreach (string s in fileStrs) {
System.IO.FileInfo info = new System.IO.FileInfo(s);
if (info.Extension == ".png" || info.Extension == ".jpg") {
string input = @"NewImg/" + info.Name;
string output = @"CompressedImg/" + info.Name;
SendReq(url, key, input, output);
}
}
Console.WriteLine("共" + fileNums + "个文件,请等待下载完成...");
Console.ReadKey(true);
}
static public async void SendReq(string url, string key, string input, string output) {
await GetCompressImg(url, key, input, output);
Console.WriteLine("已完成:" + input);
status++;
if (status == fileNums) {
Console.WriteLine("下载已全部完成,共" + status + "个文件");
}
}
static public async Task GetCompressImg(string url,string key,string input,string output){
WebClient client = new WebClient();
string auth = Convert.ToBase64String(Encoding.UTF8.GetBytes("api:" + key));
client.Headers.Add(HttpRequestHeader.Authorization, "Basic " + auth);
try {
await client.UploadDataTaskAsync(url, File.ReadAllBytes(input));
await client.DownloadFileTaskAsync(client.ResponseHeaders["Location"], output);
} catch (WebException) {
Console.WriteLine("网络请求失败:" + input);
}
}
}