/// <summary> /// 获取Bing图片。 /// </summary> /// <returns>返回BitmapImage图片</returns> public async Task <BitmapImage> GetBitmapImageAsync() { GenerateBgService bgService = new GenerateBgService(); BingImageData bingImageData = Task.Run(bgService.getBingImageDataAsync).Result; Image item = bingImageData.images[0]; Windows.Web.Http.HttpClient http = new Windows.Web.Http.HttpClient(); string uri = "https://www.bing.com" + item.url; IBuffer buffer = await http.GetBufferAsync(new Uri(uri)); BitmapImage img = new BitmapImage(); using (IRandomAccessStream stream = new InMemoryRandomAccessStream()) { await stream.WriteAsync(buffer); stream.Seek(0); await img.SetSourceAsync(stream); } // Save the bing image to local folder SaveImageToLocalFolder(uri); return(img); }
/// <summary> /// 获取Bing每日图片的Json,反序列化为类。 /// </summary> /// <returns>返回BingImageData类</returns> public async Task <BingImageData> getBingImageDataAsync() { // 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}", 1, strRegion); string strJSONString = ""; // Use Windows.Web.Http Namespace as System.Net.Http will be deprecated in future versions. HttpClient httpClient = new HttpClient(); string json = await httpClient.GetStringAsync(new Uri(strBingImageURL)); // strJson Deserialize to Json class BingImageData bingImageData = JsonConvert.DeserializeObject <BingImageData>(json); return(bingImageData); }