Results 1 to 6 of 6

Thread: Download a image and set it as your Desktop Wallpaper in C#

  1. #1
    Join Date
    Aug 2007
    Location
    in a random little world
    Posts
    5,778
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

    Default Download a image and set it as your Desktop Wallpaper in C#

    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
    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
    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;
    then you have to start the program by writing the following
    we will also import the user32.dll at the same time
    Code:
    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
    Code:
                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/wall...versal_sun.png

    now the next chunk of code
    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
    Code:
                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
    Code:
                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
    Code:
                if (FileIsThere)
                {
    now if FileIsThere is True then it will continue on with the next bit of code
    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"
    Code:
                    if (File.Exists("C:\\foxkeh\\image.png"))
                    {
    now we will check if the file we just downloaded is there
    Code:
                        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
    Code:
    SystemParametersInfo(20, 0, "C:/foxkeh/image.bmp", 0x1 | 0x2);
    this line sets image.bmp as the desktop background
    Code:
                    }
                }
            }
        }
    }
    these just close off all the open brackets

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

    ~shut

  2. #2
    Join Date
    Oct 2006
    Location
    Texas
    Posts
    1,449
    Mentioned
    1 Post(s)
    Quoted
    1 Post(s)

    Default

    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.

  3. #3
    Join Date
    Aug 2007
    Location
    in a random little world
    Posts
    5,778
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

    Default

    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

  4. #4
    Join Date
    Oct 2006
    Location
    Texas
    Posts
    1,449
    Mentioned
    1 Post(s)
    Quoted
    1 Post(s)

    Default

    Quote Originally Posted by Shuttleu View Post
    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.

  5. #5
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,553
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Nice, btw. that image link is static? besides the date etc?
    ~Hermen

  6. #6
    Join Date
    Aug 2007
    Location
    in a random little world
    Posts
    5,778
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •