I created a simple program that downloads an image (different image every day), then sets it as my desktop background. I feel like the lines of code can be cut down significantly while still remaining easy to understand.
It is a console application, and closes immediately after downloading the image and setting it as my desktop background. It runs every morning at 6:00 AM via Task Scheduler.
What improvements would you make?
using Microsoft.Win32;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Background
{
class Program
{
static void Main(string[] args)
{
using (WebClient webClient = new WebClient())
{
Rectangle resolution = Screen.PrimaryScreen.Bounds;
string json = webClient.DownloadString("super secret website url");
dynamic results = JsonConvert.DeserializeObject<dynamic>(json);
string url = "domain.com" + results.images[0].urlbase;
if (resolution.Width <= 1920 && resolution.Height <= 1200)
{
url += String.Format("_{0}x{1}.jpg", resolution.Width, resolution.Height);
}
else
{
url += "_1920x1200.jpg";
}
DesktopBackground desktopBackground = new DesktopBackground();
desktopBackground.Set(url, PicturePosition.Fill);
}
}
}
public enum PicturePosition
{
Tile, Center, Stretch, Fit, Fill
}
public class DesktopBackground
{
public DesktopBackground() { }
const int SET_DESKTOP_BACKGROUND = 20;
const int UPDATE_INI_FILE = 1;
const int SEND_WINDOWS_INI_CHANGE = 2;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
public void Set(string URL, PicturePosition style)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = httpWebReponse.GetResponseStream();
Image backgroundImage = Image.FromStream(stream);
int backgroundNumber = 0;
string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "/Backgrounds/";
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
string fullBackgroundFilePath = Path.Combine(directory, String.Format("background{0}.bmp", backgroundNumber));
while (System.IO.File.Exists(fullBackgroundFilePath))
{
backgroundNumber++;
fullBackgroundFilePath = Path.Combine(directory, String.Format("background{0}.bmp", backgroundNumber));
}
backgroundImage.Save(fullBackgroundFilePath, System.Drawing.Imaging.ImageFormat.Bmp);
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;
}
key.Close();
SystemParametersInfo(SET_DESKTOP_BACKGROUND, 0, fullBackgroundFilePath, UPDATE_INI_FILE | SEND_WINDOWS_INI_CHANGE);
}
}
}
For those who want to test it out, replace the lines between the using
condition with the following lines:
string url = "http://www.listofimages.com/wallpapers/2013/10/tarutao-marine-national-park-thailand-boats-crystal-clear-nature-landscape-1800x2880.jpg";
DesktopBackground desktopBackground = new DesktopBackground();
desktopBackground.Set(url, PicturePosition.Fill);