Get Bing Image of the Day using C# UWP Application
Overview
Bing Image of the Day has become quite popular over the years, it comprises of essentially the most spectacular user-submitted images from all over the world which have been showcased on the Bing homepage for the past 5 years. I have absolutely loved the Bing Wallpaper of the day part of the Bing desktop application. Bing desktop app changes your wallpaper every day to Bing Image of the Day, which is pretty cool and gives you a peace of mind without having to change the wallpaper every day manually. The somewhat similar concept has been used in Windows Phone as well, where you can set the lock screen to the Bing Image of the Day.
I was so interested in the idea that I wanted to try out myself building an app to retrieve the Bing Image. I knew once I could retrieve the image, I had numerous possibilities going forward. I could save it and change my desktop after a certain duration of my choice, or set it as my lock screen just as a desktop wallpaper, you get my point, the key here is to get the Bing Image of the Day.
Fortunately, Bing has a REST API
to retrieve a JSON
string which contains the URL to
the Bing Image, which we will be making use of.
That is the easy part, now the hard part is to parse and retrieve the URL from the JSON string. I will be discussing in detail the different methods that can be used for
parsing and how they can be used to extract the Bing Image URL.
Step 1. Retrieve the JSON string
The first step is to retrieve the JSON string from the Bing REST API endpoint.
We retrieve the string using an HttpClient
,we have an option of using Windows.Web.Http
or System.Net.Http
namespace.
I prefer to choose Windows.Web.Http.HttpClient for the simple reason that the System.Net.Http.HttpClient might get deprecated over time. Another reason is that
System.Net.Http.HttpClient supports only .Net Languages, whereas Windows.Web.Http.HttpClient was written with the aim of consolidating all the
HTTP APIs for different Windows Application Languages, and therefore it supports all the Windows Store
application languages. For the avid readers here is a detailed comparison between the two.
Step 2. Parse the JSON string
Now we can parse the retrieved JSON string using 3 methods
• Using Windows.Data.Json
(Native Way)
• Using LINQ-to-JSON
API’s JObject from JSON.NET
(Newtonsoft)
• Using dynamic
object and DeserializeObject
method JSON.NET (Newtonsoft)
JSON.Net has existed since long before Windows.Data.Json came into existence, however, Microsoft introduced Windows.Data.Json with native support for parsing JSON.
To use JSON.NET, you need to add the Newtonsoft
Nuget package, which adds an additional library dependency.
I personally prefer the native way, however for the purpose of this app I have discussed both the ways of parsing the JSON string using both JSON.NET and Windows.Data.Json namespaces.
Retrieved JSON string:
Step 2a. Parsing JSON string using Windows.Data.Json:
This Method parses the fetched JSON string and retrieves the Image URLs using Microsoft Windows.Data.Json
class. We use JsonObject
that implements an IMap interface, which can help in manipulating the name/value pairs like a dictionary. We use JsonObject.TryParse
method to parse the JSON string into a JSON value, it returns a boolean value, indicating success or failure.
The integral part of the retrieval is jsonObject["images"].GetArray()[i].GetObject()["url"].GetString()
.
As seen in the above JSON string, we have a root object "images"
, it further has 3 arrays, which are equal to the number of images specified in Step 1.
Each array has an object called url, which is the part of the url to the Bing Image. We retrieve url for each image and store it into a list.
Step 2b. Parsing JSON string using LINQ-to-JSON API’s JObject:
This Method parses the fetched JSON string and retrieves the Image URLs using LINQ-to-JSON
API’s
JObject
explicitly.We use this method when we don’t know the JSON structure
ahead of time. This method is recommended if only one or two values from the whole JSON string are required.
Step 2c. Parsing JSON string using DeserializeObject and dynamic object
This Method parses the fetched JSON string and retrieves the Image URLs using DeserializeObject
method.
We use JsonConvert.DeserializeObject
method when already know the JSON structure and the strongly typed class we want to deserialize into.
We use dynamic
object, as it saves us the trouble of declaring a specific class to hold the JSON object.
Step 3. Display images on UI using BitmapImage
Each URL is converted into a Bitmap Image and added into the Stack Panel dynamically to display as an Image Object.
BitmapImage
class inherits from the BitmapSource
class and is specialized
BitmapSource
that is optimized for loading images using XAML.
Working Windows 10 UWP App
Here is the screenshot of the working Windows 10 UWP app, I have hosted the Visual Studio solution file with source code at Github Repository Bing-Image-JSON which you can use to experiment yourself.
Final note:
The source code at the Github repo contains the full detailed code, which you can use to experiment and follow along. If you still face any problems or have any questions, feel free to leave a comment and I will be happy to help. Cheers :)