Last active
August 29, 2015 14:07
-
-
Save DavidMoore/46d141d664b0aa53ab84 to your computer and use it in GitHub Desktop.
Using WinRT libraries from a Windows Desktop application: Setting the LockScreen image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace WinRTTest | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using Windows.Storage; | |
using Windows.System.UserProfile; | |
class Program | |
{ | |
const string urlFormat = "http://www.bing.com{0}_{1}x{2}{3}"; | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Fetching latest Bing Daily Picture..."); | |
UpdateAsync().GetAwaiter().GetResult(); | |
Console.WriteLine("Done."); | |
} | |
static async Task UpdateAsync() | |
{ | |
using (var client = new HttpClient()) | |
using (HttpResponseMessage response = await client.GetAsync("http://www.bing.com/HPImageArchive.aspx?format=js&n=1")) | |
{ | |
BingImageArchive model = await response.Content.ReadAsAsync<BingImageArchive>(); | |
BingImage image = model.Images.Single(); | |
const int width = 1920; | |
const int height = 1080; | |
string url = string.Format(urlFormat, image.UrlBase, width, height, Path.GetExtension(image.Url)); | |
using (Stream stream = await client.GetStreamAsync(url)) | |
{ | |
var file = await KnownFolders.PicturesLibrary.CreateFileAsync("LockScreen" + Path.GetExtension(image.Url), CreationCollisionOption.ReplaceExisting); | |
using (var fileStream = await file.OpenStreamForWriteAsync()) | |
{ | |
await stream.CopyToAsync(fileStream); | |
} | |
await LockScreen.SetImageFileAsync(file); | |
} | |
} | |
} | |
} | |
class BingImageArchive | |
{ | |
public ICollection<BingImage> Images { get; set; } | |
} | |
class BingImage | |
{ | |
public string Url { get; set; } | |
public string UrlBase { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment