UWP 使用BackgroundTransfer下载图片

 0x00.添加引用

using System;
using System.Threading.Tasks;
using Windows.Networking.BackgroundTransfer;
using Windows.Storage;

0x01.下载

Uri uri = new Uri(imageUri);//需要下载图片的Uri,exp:http*****.jpg
BackgroundDownloader backgroundDownload = new BackgroundDownloader();
StorageFolder folder = await KnownFolders.PicturesLibrary.CreateFolderAsync("Acafe", CreationCollisionOption.OpenIfExists);//此处为下载路径,可是要Openfolder改为自定义路径
StorageFile newFile = await folder.CreateFileAsync(imageName, CreationCollisionOption.OpenIfExists);//创建文件,需要定义文件名
DownloadOperation download = backgroundDownload.CreateDownload(uri, newFile);//创建下载器
await download.StartAsync();//启动

 

UWP 设定图片为桌面/锁屏

0x00.添加引用

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Input;
using Windows.Storage;
using System;
using Windows.System.UserProfile;
using Windows.ApplicationModel;
using System.Collections.ObjectModel;
using Windows.UI.Xaml.Media;

0x01.选择图片

FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");

var file = await openPicker.PickSingleFileAsync();

0x02.复制所选图片并设定

if (!UserProfilePersonalizationSettings.IsSupported())
{
    MessageDialog md = new MessageDialog("Not Support");
    md.ShowAsync();
}
else
{
    StorageFolder base_folder = await Package.Current.InstalledLocation.GetFolderAsync("Assets");//选择App安装目录下Assets文件夹作为复制目录
    await file.CopyAsync(base_folder,file.Name, NameCollisionOption.ReplaceExisting);//将选择的图片复制到base_folder
    StorageFile copy_file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/" + file.Name));

    UserProfilePersonalizationSettings setting = UserProfilePersonalizationSettings.Current;
    await setting.TrySetWallpaperImageAsync(copy_file);//设置为壁纸,返回true成功,false失败
    await setting.TrySetLockScreenImageAsync(copy_file);//设置为锁屏,返回true成功,false失败
}

PS.如不将所选文件复制至App目录下会出现权限问题

C# 获取必应每日一图

 0x00.添加引用

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Windows.Networking.BackgroundTransfer;
using Windows.Storage;
using System.Xml;

0x01.模仿请求

public static Task< bool> GetBingPics()
{
    return Task.Run(async () =>
     {
         string temp = "http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1";
         string downtemp = "http://cn.bing.com";
         string baseurlexp = "//images/image/url";
         string copyrightexp = "//images/image/copyright";
         string dateexp = "//images/image/enddate";

         HttpClient request = new HttpClient();
         {
             request.DefaultRequestHeaders.Add("Accept", "application/json, text/javascript, */*; q=0.01");
             request.DefaultRequestHeaders.Add("UserAgent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0");
             request.DefaultRequestHeaders.Add("Accept-Encoding", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
             request.DefaultRequestHeaders.Add("Accept-Language", "gzip, deflate");
         }//填写请求头

         try
         {
             HttpResponseMessage response = request.GetAsync(temp).Result;
             string xmlstr = response.Content.ReadAsStringAsync().Result;
             XmlDocument doc = new XmlDocument();
             doc.LoadXml(xmlstr);

             XmlElement root = null;
             root = doc.DocumentElement;

             XmlNode xmlNode = root.SelectSingleNode(baseurlexp);
             XmlNode xmlNode1 = root.SelectSingleNode(copyrightexp);
             XmlNode xmlNode2 = root.SelectSingleNode(dateexp);
             string baseurl = xmlNode.InnerText;
             string copyrighturl = xmlNode1.InnerText;
             string date = xmlNode2.InnerText;
             string plus = "." + baseurl.Split('.')[1];

             BackgroundDownloader backgroundDownload = new BackgroundDownloader();

             StorageFolder folder = await KnownFolders.PicturesLibrary.CreateFolderAsync("BingPics", CreationCollisionOption.OpenIfExists);
             StorageFile newFile = await folder.CreateFileAsync(date + plus, CreationCollisionOption.OpenIfExists);

             Uri uri = new Uri(downtemp + baseurl);
             DownloadOperation download = backgroundDownload.CreateDownload(uri, newFile);

             await download.StartAsync();
             return true;
         }
         catch
         {
             return false;
         }
     });
}