本文為大家介紹下.NET解壓/壓縮zip文件。雖然解壓縮不是啥核心技術,但壓縮性能以及進度處理還是需要關註下,針對使用較多的zip開源組件驗證,給大家提供個技術選型參考 之前在《.NET WebSocket高併發通信阻塞問題 - 唐宋元明清2188 - 博客園 (cnblogs.com)》講過,團隊 ...
簡介
這個類封裝了必應首頁的每日壁紙查看功能,提供了查看、保存壁紙的方法,最大支持查看近8天的壁紙
使用方法
async Task Main()
{
try
{
var bing = BingWallpaperAPI.CreateInstance(8); //初始化,參數8表示一共會載入8張圖片
var task = await bing.Current();
task.Wallpaper.Dump(); //載入壁紙
Console.WriteLine("日期\t" + task.EndDate);
Console.WriteLine("標題\t" + task.Title); //載入相關信息
Console.WriteLine("版權\t" + task.Copyright);
Console.WriteLine("鏈接\t" + task.CopyrightLink);
if (await bing.SaveCurrentWallpaper(@"D:\bing.jpg")) //保存當前的壁紙
{
Console.WriteLine("保存成功");
};
var task1 = await bing.Next(); //載入下一張壁紙
task1.Wallpaper.Dump(); //列印壁紙
var task2 = await bing.Previous(); //載入上一張壁紙
task2.Wallpaper.Dump(); //列印壁紙
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
效果預覽
API封裝
點擊查看代碼
using System;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
/// <summary>
/// 載入Bing壁紙,單例,使用CreateInstance方法創建類的實例
/// </summary>
public sealed class BingWallpaperAPI
{
private static BingWallpaperAPI _instance = null;
private static readonly object _padlock = new Object();
/// <summary>
/// 創建類的實例
/// </summary>
/// <param name="imgnums">壁紙的張數,可以是1-8之間的數</param>
/// <returns></returns>
public static BingWallpaperAPI CreateInstance(byte imgnums = 3)
{
if (_instance is null)
{
lock (_padlock)
{
if (_instance is null) _instance = new BingWallpaperAPI(imgnums);
}
}
return _instance;
}
private BingWallpaperAPI(byte imgnums)
{
if (imgnums > 8 || imgnums <= 0)
{
_imgnums = 3;
Debug.WriteLine($"僅支持1-8張圖片,你輸入的{imgnums}被重置為3");
}
else { _imgnums = imgnums; }
GetWallpaperLink();
}
/// <summary>內置索引</summary>
private int _index = -1;
/// <summary>
/// 載入Bing壁紙的數量,預設為3
/// </summary>
private static byte _imgnums;
private BingApiModel _bingApiModel;
/// <summary>
/// 載入壁紙的JSON格式文件,文件中包含了壁紙的下載地址等關鍵信息
/// </summary>
/// <exception cref="Exception"></exception>
private void GetWallpaperLink()
{
HttpClient client = new HttpClient();
for (int i = 0; i < 3; i++) //這個步驟會嘗試3次
{
try
{
var res = client.GetAsync($"https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n={_imgnums}&mkt=zh-CN").Result;
//參數n:1-8 返回請求數量,目前最多一次獲取8張
//參數format:js/xml
//參數idx:請求圖片截止天數 0 今天;1 截止至昨天,類推(目前最多獲取到7天前的圖片)
//參數mkt:地區
var json = res.Content.ReadAsStringAsync().Result;
BingApiModel bingApiModel = new BingApiModel();
_bingApiModel = JsonSerializer.Deserialize<BingApiModel>(json);
ImageCount = _bingApiModel.images.Count();
if (ImageCount == _imgnums)
{
break; //載入成功時直接終止迴圈
}
}
catch (Exception ex)
{
if (i == 2) { throw ex; } else Console.WriteLine(ex.Message);
}
}
}
/// <summary>
/// 根據JSON數據下載壁紙
/// </summary>
/// <param name="index">_bingApiModel索引</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
private async Task<BingWallpaper> GetWallpaper(int index)
{
try
{
using (HttpClient client = new HttpClient())
{
var element = _bingApiModel.images[index];
var res = await client.GetAsync("https://cn.bing.com" + element.url);
//這個URL中的第一個1920x1080改成UHD是4k
var imgStream = await res.Content.ReadAsStreamAsync();
return (new BingWallpaper()
{
Wallpaper = System.Drawing.Image.FromStream(imgStream),
Title = element.title,
Copyright = element.copyright,
CopyrightLink = element.copyrightlink,
EndDate = element.enddate,
});
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 載入JSON數據包含的URL數量
/// </summary>
public int ImageCount { get;private set; }
/// <summary>
/// 下一張壁紙
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<BingWallpaper> Next()
{
if (ImageCount == 0)
{
return null;
}
_index++;
if (_index >= ImageCount) //重置索引
{
_index = 0;
}
return await GetWallpaper(_index);
}
/// <summary>
/// 上一張壁紙
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<BingWallpaper> Previous()
{
if (ImageCount == 0)
{
return null;
}
_index--;
if (_index < 0)
{
_index = ImageCount - 1; //重置索引
}
return await GetWallpaper(_index);
}
/// <summary>
/// 保存當前的壁紙到本地
/// </summary>
/// <param name="path">保存的路徑、含文件名(如c:\1.jpg),格式為JPEG</param>
/// <returns>保存是否成功</returns>
/// <exception cref="Exception"></exception>
public async Task<bool> SaveCurrentWallpaper(string path)
{
try
{
var task = await Current();
task.Wallpaper.Save(path, ImageFormat.Jpeg);
return true;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 當前壁紙
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<BingWallpaper> Current()
{
if (ImageCount == 0)
{
return null;
}
if (_index == -1)
{
_index = 0;
}
return await GetWallpaper(_index);
}
}
/// <summary>
/// 用來保存壁紙信息的類模型
/// </summary>
public class BingWallpaper
{
/// <summary>壁紙</summary>
public System.Drawing.Image Wallpaper { get; set; }
/// <summary>標題</summary>
public string Title { get; set; }
/// <summary>版權</summary>
public string Copyright { get; set; }
/// <summary>版權鏈接</summary>
public string CopyrightLink { get; set; }
/// <summary>
/// 壁紙所屬日期
/// </summary>
public string EndDate { get; set; }
}
/// <summary>
/// 用來解析JSON數據的類模型
/// </summary>
public class BingApiModel
{
/// <summary>
/// 返回圖片集合
/// </summary>
public Image[] images { get; set; }
}
public class Image
{
/// <summary>
/// 每日壁紙的日期
/// </summary>
public string enddate { get; set; }
/// <summary>
/// 壁紙的地址
/// </summary>
public string url { get; set; }
/// <summary>壁紙的簡述、拍攝地址(攝影人)</summary>
public string copyright { get; set; }
/// <summary>壁紙相關搜索鏈接</summary>
public string copyrightlink { get; set; }
/// <summary>壁紙的標題</summary>
public string title { get; set; }
}
原創聲明
作者:落水的月亮
出處:博客園
本文版權歸作者和博客園共有,歡迎轉載,轉載請務必在文章明顯位置標出作者及原文鏈接,謝謝。