• 使用TinyPNG提供的API,对图片进行压缩(C#)


    项目需要,经常需要手动压缩图片,流程太过麻烦,效率低下。所以写了一个小程序,以提高工作效率
    1. using System;
    2. using System.Net;
    3. using System.Text;
    4. using System.IO;
    5. class Program
    6. {
    7. static void Main()
    8. {
    9. Console.WriteLine("请输入TinyPng.com的API KEY,获取地址:https://tinypng.com/developers");
    10. string key = Console.ReadLine();
    11. if (!System.IO.Directory.Exists("NewImg"))
    12. {
    13. System.IO.Directory.CreateDirectory("NewImg");
    14. Console.WriteLine("已创建NewImg目录,请把需要处理的图片放到该目录下");
    15. Console.ReadKey(true);
    16. return;
    17. }
    18. if (!System.IO.Directory.Exists("CompressedImg")){
    19. System.IO.Directory.CreateDirectory("CompressedImg");
    20. }
    21. string url = "https://api.tinify.com/shrink";
    22. string[] fileStrs = System.IO.Directory.GetFiles("NewImg");
    23. Console.WriteLine("共" + fileStrs.Length + "个文件,请等待下载完成...");
    24. int num = 0;
    25. foreach (string s in fileStrs)
    26. {
    27. System.IO.FileInfo info = new System.IO.FileInfo(s);
    28. if (info.Extension == ".png" || info.Extension == ".jpg")
    29. {
    30. string input = @"NewImg/" + info.Name;
    31. string output = @"CompressedImg/" + info.Name;
    32. GetCompressImg(url, key, input, output);
    33. num++;
    34. }
    35. }
    36. Console.WriteLine("文件已下载到CompressedImg文件夹下,共" + num + "个文件");
    37. Console.ReadKey(true);
    38. }
    39. static public void GetCompressImg(string url,string key,string input,string output){
    40. WebClient client = new WebClient();
    41. string auth = Convert.ToBase64String(Encoding.UTF8.GetBytes("api:" + key));
    42. client.Headers.Add(HttpRequestHeader.Authorization, "Basic " + auth);
    43. try
    44. {
    45. client.UploadData(url, File.ReadAllBytes(input));
    46. client.DownloadFile(client.ResponseHeaders["Location"], output);
    47. }
    48. catch (WebException)
    49. {
    50. Console.WriteLine("网络请求失败:" + input);
    51. }
    52. }
    53. }

    上面的程序在下载期间会阻塞主线程,效率不够高。
    使用await/async改写成异步模式。

    1. using System;
    2. using System.Net;
    3. using System.Text;
    4. using System.IO;
    5. using System.Threading.Tasks;
    6. class Program
    7. {
    8. static public int fileNums = 0;
    9. static public int status = 0;
    10. static void Main()
    11. {
    12. Console.WriteLine("请输入TinyPng.com的API KEY,获取地址:https://tinypng.com/developers");
    13. string key = Console.ReadLine();
    14. if (!System.IO.Directory.Exists("NewImg"))
    15. {
    16. System.IO.Directory.CreateDirectory("NewImg");
    17. Console.WriteLine("已创建NewImg目录,请把需要处理的图片放到该目录下");
    18. Console.ReadKey(true);
    19. return;
    20. }
    21. if (!System.IO.Directory.Exists("CompressedImg")){
    22. System.IO.Directory.CreateDirectory("CompressedImg");
    23. }
    24. string url = "https://api.tinify.com/shrink";
    25. string[] fileStrs = System.IO.Directory.GetFiles("NewImg");
    26. fileNums = fileStrs.Length;
    27. foreach (string s in fileStrs) {
    28. System.IO.FileInfo info = new System.IO.FileInfo(s);
    29. if (info.Extension == ".png" || info.Extension == ".jpg") {
    30. string input = @"NewImg/" + info.Name;
    31. string output = @"CompressedImg/" + info.Name;
    32. SendReq(url, key, input, output);
    33. }
    34. }
    35. Console.WriteLine("共" + fileNums + "个文件,请等待下载完成...");
    36. Console.ReadKey(true);
    37. }
    38. static public async void SendReq(string url, string key, string input, string output) {
    39. await GetCompressImg(url, key, input, output);
    40. Console.WriteLine("已完成:" + input);
    41. status++;
    42. if (status == fileNums) {
    43. Console.WriteLine("下载已全部完成,共" + status + "个文件");
    44. }
    45. }
    46. static public async Task GetCompressImg(string url,string key,string input,string output){
    47. WebClient client = new WebClient();
    48. string auth = Convert.ToBase64String(Encoding.UTF8.GetBytes("api:" + key));
    49. client.Headers.Add(HttpRequestHeader.Authorization, "Basic " + auth);
    50. try {
    51. await client.UploadDataTaskAsync(url, File.ReadAllBytes(input));
    52. await client.DownloadFileTaskAsync(client.ResponseHeaders["Location"], output);
    53. } catch (WebException) {
    54. Console.WriteLine("网络请求失败:" + input);
    55. }
    56. }
    57. }





  • 相关阅读:
    读javascript高级程序设计08-引用类型之Global、Math、String
    读javascript高级程序设计07-引用类型、Object、Array
    读javascript高级程序设计06-面向对象之继承
    读javascript高级程序设计05-面向对象之创建对象
    读javascript高级程序设计04-canvas
    读javascript高级程序设计03-函数表达式、闭包、私有变量
    读javascript高级程序设计02-变量作用域
    C#将Word转换成PDF方法总结(基于Office和WPS两种方案)
    【转】 C#中Finally的一个不太常见的用法
    一看就懂的ReactJs入门教程-精华版
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/c4e38db51d593f3f6e49bfb0e5421e25.html
Copyright © 2020-2023  润新知