I've created a simple command-line program that downloads the Bing Image of the Day, then sets it as my desktop background.
It is a console application, and closes immediately after setting my desktop background. It runs daily via Task Scheduler.
I've been working on this as a mini project for a while and would like to know if I am employing any poor coding practices or if there are ways to improve efficiency. The project can be found on GitHub. The executable is located at BingBackground/BingBackground/BingBackground/bin/Release/BingBackground.exe
static void Main(String[] args)
{
String urlBase = GetBackgroundURLBase();
Image background = DownloadBackground(urlBase + GetResolutionExtension(urlBase));
SaveBackground(background);
SetBackground(background, PicturePosition.Fill);
}
public static String GetBackgroundURLBase()
{
using (WebClient webClient = new WebClient())
{
Console.WriteLine("Downloading JSON...");
String jsonString = webClient.DownloadString("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US");
dynamic jsonObject = JsonConvert.DeserializeObject<dynamic>(jsonString);
return "https://www.bing.com" + jsonObject.images[0].urlbase;
}
}
public static String GetBackgroundTitle()
{
using (WebClient webClient = new WebClient())
{
String jsonString = webClient.DownloadString("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US");
dynamic jsonObject = JsonConvert.DeserializeObject<dynamic>(jsonString);
String copyrightText = jsonObject.images[0].copyright;
return copyrightText.Substring(0, copyrightText.IndexOf(" ("));
}
}
public static Boolean WebsiteExists(String url)
{
try
{
WebRequest request = WebRequest.Create(url);
request.Method = "HEAD";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return (response.StatusCode == HttpStatusCode.OK);
}
catch
{
return false;
}
}
public static String GetResolutionExtension(String url)
{
Rectangle resolution = Screen.PrimaryScreen.Bounds;
String potentialExtension = "_" + resolution.Width + "x" + resolution.Height + ".jpg";
if (WebsiteExists(url + potentialExtension))
{
Console.WriteLine("Background for " + resolution.Width + "x" + resolution.Height + " found.");
return potentialExtension;
}
else
{
Console.WriteLine("No background for " + resolution.Width + "x" + resolution.Height + " was found.");
Console.WriteLine("Using 1920x1080 instead.");
return "_1920x1080.jpg";
}
}
public static Image DownloadBackground(String url)
{
Console.WriteLine("Downloading background...");
WebRequest request = WebRequest.Create(url);
WebResponse reponse = request.GetResponse();
Stream stream = reponse.GetResponseStream();
return Image.FromStream(stream);
}
public static String GetBackgroundImagePath()
{
String directory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "/Bing Backgrounds/" + DateTime.Now.Year.ToString();
Directory.CreateDirectory(directory);
return Path.Combine(directory, DateTime.Now.ToString("M-d-yyyy") + ".bmp");
}
public static void SaveBackground(Image background)
{
Console.WriteLine("Saving background...");
background.Save(GetBackgroundImagePath(), System.Drawing.Imaging.ImageFormat.Bmp);
}
public enum PicturePosition
{
Tile,
Center,
Stretch,
Fit,
Fill
}
internal sealed class NativeMethods
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern int SystemParametersInfo(int uAction, int uParam, String lpvParam, int fuWinIni);
}
public static void SetBackground(Image background, PicturePosition style)
{
Console.WriteLine("Setting background...");
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true))
{
switch (style)
{
case PicturePosition.Tile:
key.SetValue("PicturePosition", "0");
key.SetValue("TileWallpaper", "1");
break;
case PicturePosition.Center:
key.SetValue("PicturePosition", "0");
key.SetValue("TileWallpaper", "0");
break;
case PicturePosition.Stretch:
key.SetValue("PicturePosition", "2");
key.SetValue("TileWallpaper", "0");
break;
case PicturePosition.Fit:
key.SetValue("PicturePosition", "6");
key.SetValue("TileWallpaper", "0");
break;
case PicturePosition.Fill:
key.SetValue("PicturePosition", "10");
key.SetValue("TileWallpaper", "0");
break;
}
}
const int SET_DESKTOP_BACKGROUND = 20;
const int UPDATE_INI_FILE = 1;
const int SEND_WINDOWS_INI_CHANGE = 2;
NativeMethods.SystemParametersInfo(SET_DESKTOP_BACKGROUND, 0, GetBackgroundImagePath(), UPDATE_INI_FILE | SEND_WINDOWS_INI_CHANGE);
}