Compress pictures using the APIs provided by Tinypng (C #)

Source: Internet
Author: User

Project needs, often need to manually compress pictures, the process is too cumbersome, inefficient. So write a small program to improve productivity
  
 
  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 Span class= "pun". 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. }



Null

Compress pictures using the APIs provided by Tinypng (C #)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.