Click here to Skip to main content
15,354,641 members
Articles / Multimedia / GDI+
Posted 12 Apr 2005


52 bookmarked

WallPaper Changer for .NET

Rate me:
Please Sign up or sign in to vote.
4.61/5 (13 votes)
29 Apr 20053 min read
An article about changing the desktop wallpaper.

Sample Image


This is the second version to the application. I made changes according to the remarks posted by the viewers.

This is an attempt to create a simple application that has a simple GUI and still does all the desired functionality. I wanted an application that collects the images by itself and spares me that feature.

The following subjects are discussed in this article:

  1. Setting a wallpaper using WinAPI.
  2. Using xmlWriter for file writing.
  3. Image File type changing.
  4. Getting all the files from a directory recursively.
  5. Separating files from directories in a collection.
  6. Implementing Drag & Drop scenario.
  7. Showing the application in the traybar.


To change the Desktop background, the application needs to use the WinAPI SystemParametersInfo and supply it with the desired image parameters. The only problem is that the API accepts only the BMP image type. My solution was to create a BMP from each JPG or JPEG file in the directory.

Using the code

  1. Setting a wallpaper using WinAPI:

    The API function "SystemParametersInfo" has many usages. I will use the SPI_SETDESKWALLPAPER parameter which tells the system to change the wallpaper. The SPIF_SENDCHANGE parameter tells the system to send all the open windows an acknowledgement message about the wallpaper changing.

    nResult = WinAPI.SystemParametersInfo(SPI_SETDESKWALLPAPER, 
                                 1, fileName, SPIF_SENDCHANGE);
  2. Using xmlTextWriter for file writing:

    My xml file holds the last selected image that was wallpapered. After each wallpaper changing, the xml file will be updated so that the next time the application uses the proper image. The xmlTextwriter must start with writer.WriteStartElement and end with writer.WriteEndElement. In the middle, each element is added using its name and value: "picIndex", tempPicIndex.ToString(). The flush method forces the writer to immediately write the data. The Close method closes the writer.

    XmlTextWriter writer = 
           new XmlTextWriter(m_settingsPath + "\\settings.xml", null);
    writer.Formatting = Formatting.Indented;
    writer.Indentation = 4;
    writer.WriteComment("Settings for WallPaper changer program");
    writer.WriteElementString("picIndex", tempPicIndex.ToString());
    writer.WriteElementString("Interval", tempGlobalInterval.ToString());
  3. Image File type changing:

    First, you need to create a BMP file in the memory using the Bitmap constructor, and then you need to save the new file to the local HD.

    Bitmap theImage = new Bitmap(fileName);
    theImage.Save (localPath + "\\new.bmp",
  4. Getting all the files from a directory recursively:

    In order to get all the files from all sub-directories, the application needs to use the Directory class. I used a recursive method to gather all files. I got all the files from each sub-directory to a string array and added each one of them to the ListBox.

    string[] dirs = Directory.GetFiles(localPath , fileType);
    foreach (string dir in dirs){
        mycheckedListBox.Items.Add (dir ,CheckState.Checked);
  5. Separating file from directories in a collection:

    For each of the files, the application checks if it is a file or a directory by the FileAttributes property. In the following code, the application casts the string named dir and checks if it is file or directory by its fileattributes.Directory flag:

    if (File.GetAttributes(dir) == FileAttributes.Directory) bAns = true;
  6. Implementing Drag & Drop scenario:

    First of all, in the Form Designer mode, the listbox.allowDrag and the form.allowDrag must be set to true. The next step is to change the DragAndDrop effect (and the icon of the cursor) and allow the drop scenario to continue. Otherwise the DragDrop event will never be reached.

    private void mycheckedListBox_DragEnter(object sender, 
                       System.Windows.Forms.DragEventArgs e) 
      if (e.Data.GetDataPresent(DataFormats.FileDrop, false)==true) 
          e.Effect = DragDropEffects.All;

    Finally, I have implemented the DragDrop event to get all the files to a string ArrayList:

    private void mycheckedListBox_DragDrop(object sender, 
                                System.Windows.Forms.DragEventArgs e)
       string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
       foreach (string dirfile in files) { ....}
  7. Showing the application in the traybar:

    C# has wrapped all the procedures to a very simple one. All you need to do is add notifyIcon to your application's form and to relate it to contextMenu. The next step is to implement the Form.resize and make sure that when minimized the window will disappear:

    private void Form1_Resize(object sender, System.EventArgs e)
      if (WindowState == FormWindowState.Minimized) 

    You will probably want to implement the notifyIcon.DoubleClick also.

    private void notifyIcon1_DoubleClick(object sender, System.EventArgs e) 
      this.WindowState = FormWindowState.Normal;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

arbel kfir
Web Developer
Israel Israel
partying, girls, partying, girls

and a bit of c#

homepage : http:\\

Comments and Discussions

Generalhi Pin
sandeepparekh2-Jun-11 21:19
Membersandeepparekh2-Jun-11 21:19 
Generalno worky with my redirected desktop and offline files.. Pin
ehp39-Oct-07 6:04
Memberehp39-Oct-07 6:04 
GeneralI want to have a calendar in desktop Pin
DungVinh17-Aug-06 20:54
MemberDungVinh17-Aug-06 20:54 
QuestionCentered Image ? Pin
Antheo15-Jul-05 6:15
MemberAntheo15-Jul-05 6:15 
AnswerRe: Centered Image ? Pin
tombala20-Feb-06 8:07
Membertombala20-Feb-06 8:07 
AnswerRe: Centered Image ? Pin
tombala20-Feb-06 8:34
Membertombala20-Feb-06 8:34 
GeneralRe: Centered Image ? Pin
tombala20-Feb-06 9:09
Membertombala20-Feb-06 9:09 
QuestionMemory? Pin
Anonymous12-Jul-05 5:46
MemberAnonymous12-Jul-05 5:46 
AnswerRe: Memory? Pin
kfir arbel25-Aug-05 4:15
susskfir arbel25-Aug-05 4:15 
GeneralRe: Memory? Pin
Santosh K Sahoo7-Aug-06 20:08
MemberSantosh K Sahoo7-Aug-06 20:08 
GeneralRe: Memory? Pin
askhenry11-Aug-07 4:48
Memberaskhenry11-Aug-07 4:48 
GeneralRecursive Directory Import Pin
Sven Rieke12-Apr-05 22:22
MemberSven Rieke12-Apr-05 22:22 
GeneralRe: Recursive Directory Import Pin
Jens Scheidtmann13-Apr-05 3:17
MemberJens Scheidtmann13-Apr-05 3:17 
GeneralRe: Recursive Directory Import Pin
Jens Scheidtmann13-Apr-05 3:18
MemberJens Scheidtmann13-Apr-05 3:18 
GeneralRe: Recursive Directory Import Pin
morphix13-Apr-05 21:45
Membermorphix13-Apr-05 21:45 
GeneralRe: Recursive Directory Import Pin
arbel kfir14-Apr-05 9:06
Memberarbel kfir14-Apr-05 9:06 
GeneralRe: Recursive Directory Import Pin
james32517-Apr-05 14:43
Memberjames32517-Apr-05 14:43 
GeneralRe: Recursive Directory Import Pin
Vladislav Borovikov2-May-05 3:41
MemberVladislav Borovikov2-May-05 3:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.