Get,Bing,Image,Day,using,c#

Mert 8/23/2016 0

Get Bing Image of the Day using C# 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. 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.

JavaScript
 // We can specify the region we want for the Bing Image of the Day.
string strRegion = "en-US";
string strBingImageURL = string.Format("http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n={0}&mkt={1}", _numOfImages, strRegion);
string strJSONString = "";

HttpClient client = new HttpClient();

// Using an Async call makes sure the app is responsive during the time the response is fetched.
// GetAsync sends an Async GET request to the Specified URI.
HttpResponseMessage response = await client.GetAsync(new Uri(strBingImageURL));

// Content property get or sets the content of a HTTP response message. 
// ReadAsStringAsync is a method of the HttpContent which asynchronously 
// reads the content of the HTTP Response and returns as a string.
strJSONString = await response.Content.ReadAsStringAsync(); 


JavaScript
 {"images":[

    {"startdate":"20160719","fullstartdate":"201607190700","enddate":"20160720","url":"/az/hprichbg/rb/NeonMuseum_EN-US8131993872_1920x1080.jpg","urlbase":"/az/hprichbg/rb/NeonMuseum_EN-US8131993872","copyright":"Neon Museum, Las Vegas, Nevada (© Kerrick James/Getty Images)","copyrightlink":"http://www.bing.com/search?q=neon museum&form=hpcapt&filters=HpDate:%2220160719_0700%22","wp":false,"hsh":"96d3e7bbe87ad583a17ef765ed935d8f","drk":1,"top":1,"bot":1,"hs":[]}
    ,
    {"startdate":"20160718","fullstartdate":"201607180700","enddate":"20160719","url":"/az/hprichbg/rb/DiamondHead_EN-US10119574903_1920x1080.jpg","urlbase":"/az/hprichbg/rb/DiamondHead_EN-US10119574903","copyright":"Aerial view of Diamond Head, O’ahu, Hawaii (© Biederbick & Rumpf/Offset)","copyrightlink":"http://www.bing.com/search?q=diamond head hawaii&form=hpcapt&filters=HpDate:%2220160718_0700%22","wp":true,"hsh":"1aff492416f5e2fcf6e898743154fbda","drk":1,"top":1,"bot":1,"hs":[]}
    ,
    {"startdate":"20160717","fullstartdate":"201607170700","enddate":"20160718","url":"/az/hprichbg/rb/MuizenbergSA_EN-US9176714978_1920x1080.jpg","urlbase":"/az/hprichbg/rb/MuizenbergSA_EN-US9176714978","copyright":"Beach huts in Muizenberg, South Africa (© Getty Images)","copyrightlink":"http://www.bing.com/search?q=Muizenberg, South Africa&form=hpcapt&filters=HpDate:%2220160717_0700%22","wp":true,"hsh":"8d4bf37f9d61ab103283005264b4b8cb","drk":1,"top":1,"bot":1,"hs":[]}

    ]

    ,"tooltips":{"loading":"Loading...","previous":"Previous image","next":"Next image","walle":"This image is not available to download as wallpaper.","walls":"Download this image. Use of this image is restricted to wallpaper only."} 


// Parse using Windows.Data.Json. JsonObject jsonObject; bool boolParsed = JsonObject.TryParse(_strRawJSONString, out jsonObject); if (boolParsed) { for (int i = 0; i < _numOfImages; i ) { _lstBingImageURLs.Add(jsonObject["images"].GetArray()[i].GetObject()["url"].GetString()); } }


// Parse using LINQ-to-JSON API's JObject explicitly. JObject jResults = JObject.Parse(_strRawJSONString); foreach (var image in jResults["images"]) { _lstBingImageURLs.Add((string)image["url"]); }


// Parse using LINQ-to-JSON API's JObject explicitly. JObject jResults = JObject.Parse(_strRawJSONString); foreach (var image in jResults["images"]) { _lstBingImageURLs.Add((string)image["url"]); }


// Parse using DeserializeObject method. for (int i = 0; i < _numOfImages; i ) { _lstBingImageURLs.Add(Convert.ToString((JsonConvert.DeserializeObject<dynamic>(_strRawJSONString)).images[i].url)); }


foreach (string url in _lstBingImageURLs) { // We use the Image control to display an image on the UI. Image imgbingImage = new Image(); var bingURL = "https://www.bing.com" url; BitmapSource imgbingImageSource = new BitmapImage(new Uri(bingURL)); imgbingImage.Source = imgbingImageSource; // Add the Image control to the Stack Panel spImages.Children.Add(imgbingImage); }

Report Bug

Please Login to Report Bug

Reported Bugs

Comments

Please Login to Comment

Comments