PDA

View Full Version : Download a image and set it as your Desktop Wallpaper in C#



Shuttleu
08-17-2009, 03:39 AM
well i made this program for my little brother as he didnt want to go to the site every time there was a new wallpaper that came out
so i thought that i would write it up and show it to you so you can create one for yourself

so here is the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Text;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;

namespace ConsoleApplication1
{
class Program
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

static void Main(string[] args)
{

bool FileIsThere;
string TheURL = "http://www.foxkeh.com/downloads/wallpapers/";
DateTime dt = DateTime.Now;
int TheYear = dt.Year;
int TheMonth = dt.Month;
string TheDate = TheYear.ToString();
if (TheMonth < 10)
TheDate += "0";
TheDate += TheMonth.ToString();
TheURL += TheDate + "/1280x1024_cal_universal_sun.png";

WebRequest request = WebRequest.Create(new Uri(TheURL));
request.Method = "HEAD";

WebResponse response;
try
{
response = request.GetResponse();
FileIsThere = true;
}
catch (Exception)
{
FileIsThere = false;
}

if (!System.IO.Directory.Exists(@"C:\\foxkeh"))
System.IO.Directory.CreateDirectory(@"C:\\foxkeh");

if (FileIsThere)
{
WebClient Client = new WebClient();
Client.DownloadFile(TheURL, "C:/foxkeh/image.png");
if (File.Exists("C:\\foxkeh\\image.png"))
{
Image Dummy = Image.FromFile("C:\\foxkeh\\image.png");
Dummy.Save("C:\\foxkeh\\image.bmp", ImageFormat.Bmp);
SystemParametersInfo(20, 0, "C:/foxkeh/image.bmp", 0x1 | 0x2);
}
}
}
}
}


now this is mostly self explanatry

first you have to import all the thing that are needed in order to run the program

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Text;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;

then you have to start the program by writing the following
we will also import the user32.dll at the same time

namespace ConsoleApplication1
{
class Program
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

static void Main(string[] args)
{
then we will declare the variables that are needed and set them

bool FileIsThere;
string TheURL = "http://www.foxkeh.com/downloads/wallpapers/";
DateTime dt = DateTime.Now;
int TheYear = dt.Year;
int TheMonth = dt.Month;
string TheDate = TheYear.ToString();
if (TheMonth < 10)
TheDate += "0";
TheDate += TheMonth.ToString();
TheURL += TheDate + "/1280x1024_cal_universal_sun.png";
in this you are creating a boolean which is called FileIsThere, we will use this to see if the new updated image is there or not

we then have a string which will then hold the web address, but because we want to add the date in, we havent put the whole address in, so so far is is set to http://www.foxkeh.com/downloads/wallpapers/

then we create a DateTime called dt, this will hold both the date and the time that your computer is currently set to

The Year and The Month are both integers, and as you can probably guess they will hold the year and the month

now we will convert TheYear into a string and store it in the variable called The Date

then it will check if The Month is less than 10, if it is then it will add a 0 to TheDate so that if the month is 3 then it will put 03 into the address instead of 3
after that it will change TheMonth to a string and add it to TheDate

then it will add TheDate to TheURL, which is the address that it should get the image from, so it is now http://www.foxkeh.com/downloads/wallpapers/200908

then it will add the last of the address onto TheURL, so it is now http://www.foxkeh.com/downloads/wallpapers/200908/1280x1024_cal_universal_sun.png

now the next chunk of code

WebRequest request = WebRequest.Create(new Uri(TheURL));
request.Method = "HEAD";

WebResponse response;
here we create a WebRequest called request and a WebResponse called response

try
{
response = request.GetResponse();
FileIsThere = true;
}
catch (Exception)
{
FileIsThere = false;
}
firstly it will try to see if there is something at the address that has been provided, and if there is then it will set FileIsThere to true, otherwise it will set it to False

if (!System.IO.Directory.Exists(@"C:\\foxkeh"))
System.IO.Directory.CreateDirectory(@"C:\\foxkeh");
this will check if the directory "C:/foxkeh" exists, if it doesnt then it will create it


if (FileIsThere)
{
now if FileIsThere is True then it will continue on with the next bit of code

WebClient Client = new WebClient();
Client.DownloadFile(TheURL, "C:/foxkeh/image.png");
this will create a WebClient so that we can download the file that we want to set to the desktop background, then it will download it to "C:/foxkeh/image.png"

if (File.Exists("C:\\foxkeh\\image.png"))
{now we will check if the file we just downloaded is there

Image Dummy = Image.FromFile("C:\\foxkeh\\image.png");
Dummy.Save("C:\\foxkeh\\image.bmp", ImageFormat.Bmp);
now this will create a Image and load foxkeh.png into it and then save it as image.bmp, because you cannot set a png as the desktop background

SystemParametersInfo(20, 0, "C:/foxkeh/image.bmp", 0x1 | 0x2);this line sets image.bmp as the desktop background

}
}
}
}
}these just close off all the open brackets

now as you have probably guessed im not very good at writing tutorials...

~shut

Buckleyindahouse
08-17-2009, 03:41 AM
Looks good, but surround any IO Operations or Internet Operations with a try and catch statement, else if it times out, 404 error or file in use error, then it's bye bye program.

Shuttleu
08-17-2009, 03:48 AM
ok but if i did a try catch statement to see if the file exists so it wouldnt crash while downloading the image because it would only try to download it if it exists
and how can it have a file in use error if it is just checking if a folder exists or if its creating it?

~shut

Buckleyindahouse
08-17-2009, 04:00 AM
ok but if i did a try catch statement to see if the file exists so it wouldnt crash while downloading the image because it would only try to download it if it exists
and how can it have a file in use error if it is just checking if a folder exists or if its creating it?

~shut
File in use isn't the only IO error there is.
Basically, I'm just saying as a rule of thumb is to surround internet and IO parts with a try and catch.

ShowerThoughts
08-17-2009, 05:13 AM
Nice, btw. that image link is static? besides the date etc?

Shuttleu
08-17-2009, 06:16 AM
Nice, btw. that image link is static? besides the date etc?
yes that link is static apart from the date
my little brother has been using it for about 6 months and has not had a problem with it

~shut