Introduction
Microsoft’s Bing has some amazing images as backgrounds; however, if you want to use them as background images for your Windows Desktop, you must go through the tedious process of opening your browser and saving the image as a background each day.
Unless you can automate the process that is.
Finding the images
The first step in the process is to find the images. Since they are the background on the webpage, the image must be downloaded to your Temporary Internet Files folder; however, these files have sometimes cryptic names, can be removed when cleaning the temporary files, and of course, you must visit Bing to initiate the download.
Long Zheng at www.istartedsomething.com has created an excellent archive of all Bing images, yet the problem is, through there is an RSS feed available, they are not downloadable directly from this site. There are instructions available here: http://www.makeuseof.com/tag/how-to-set-a-bing-wallpaper-desktop-slideshow-in-windows-7/ on creating a Theme file using the RSS feed; however, I’ve found this method to be unreliable.
Through some investigation, I found the image information is available from Bing using this URL: http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-US.
As the format parameter indicates, this will return an XML formatted stream that contains information such as the date the images is for, the relative URL, description, and copyright information.
The idx parameter tells where you want to start from. 0 would start at the current day, 1 the previous day, 2 the day after that, etc. For instance, if the date were 1/30/2011, using idx = 0, the file would start with 20110130; using idx = 1, it would start with 20110129; and so forth.
The n parameter tells how many images to return. n = 1 would return only one, n = 2 would return two, and so on.
The mkt parameter tells which of the eight markets Bing is available for you would like images from. The valid values are: en-US, zh-CN, ja-JP, en-AU, en-UK, de-DE, en-NZ, en-CA.
Automating the download
Now that you know where to get the images from, it is easy to create an automated process to download them.
public class BingImages
{
private const string DOWNLOAD_PATH = @"D:\BingImages";
private const string BING = "http://www.bing.com";
private const string IMG_URL = "http://www.bing.com/HPImageArchive" +
".aspx?format=xml&idx=0&n={0}&mkt={1}";
private static string[] Markets = new string[] { "en-US", "zh-CN",
"ja-JP", "en-AU", "en-UK",
"de-DE", "en-NZ", "en-CA" };
private const int NUMBER_OF_IMAGES = 1;
public static void DownLoadImages()
{
ValidateDownloadPath();
XDocument doc = null;
WebRequest request = null;
foreach (string market in Markets)
{
string url = string.Format(IMG_URL, NUMBER_OF_IMAGES, market);
request = WebRequest.Create(url);
using (Stream stream = request.GetResponse().GetResponseStream())
{
doc = XDocument.Load(stream);
}
foreach (XElement image in doc.Descendants("image"))
{
SaveImage(image.Element("url").Value);
}
}
}
private static void SaveImage(string url)
{
string filename = GetImageName(url);
if (!File.Exists(filename))
{
WebRequest request = WebRequest.Create(BING + url);
using (Stream stream = request.GetResponse().GetResponseStream())
{
Image img = Image.FromStream(stream);
img.Save(filename);
}
}
}
private static string GetImageName(string url)
{
Regex reg = new Regex(@"[0-9]+\w");
Match m = reg.Match(url);
return string.Format(@"{0}\{1}.jpg", DOWNLOAD_PATH, m.Value);
}
private static void ValidateDownloadPath()
{
if (!Directory.Exists(DOWNLOAD_PATH))
{
Directory.CreateDirectory(DOWNLOAD_PATH);
}
}
}
With this code, you can create a Windows Service or any other type of application to automate the download process, then set the Windows Desktop backgrounds to use the designated folder.