这个类封装了必应首页的每日壁纸查看功能,提供了查看、保存壁纸的方法,最大支持查看近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);
}
}
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; }
}
作者:落水的月亮
出处:博客园
转载:可以转载,转载必须注明出处。必须在文章中给出原文连接。