Click here to Skip to main content
15,881,089 members
Articles / Desktop Programming / Win32

Wallpaper Changer Prank

Rate me:
Please Sign up or sign in to vote.
4.77/5 (13 votes)
3 Dec 2013CPOL9 min read 27.6K   491   9   6
An app to periodically change the desktop's wallpaper

Introduction

In our office, we have established the policy to lock our computer (Windows key + L) when we are away from it. If someone forgets to lock his/her computer, as part of the policy we have agreed, the remaining coworkers can make changes to the computer’s settings (typically changing the desktop wallpaper). When the owner of the computer comes back, he knows that he forgot to lock the computer and also has a good time laughing about the prank. But, what if we could automate the process of changing someone else’s wallpaper? Furthermore, what if we could have a background process making the change whenever we want.

Background

There are quite a few articles around the web about changing a computer’s wallpaper describing the steps and providing sample code to achieve it. In this article, I’ll be using C# and calling Win32 APIs to change the computer’s wallpaper. Next, I’ll be discussing different approaches to achieve the automation and periodical repetition of changing the computer’s wallpaper.

For the section about setting the wallpaper, I’ll be using documentation from other sites (which you can find at the end of this article, in the reference section). But mainly, I’ll be using the method described by Sean Campbell in his article Setting Wallpaper (link to the article provided below).

Getting the Image to Display

As a first step, we need to choose the image we want to display as new wallpaper. We can either provide the path to an image we already have stored. We can provide a URL or a set of URLs to the image(s) we want to display. However, since this is a prank, not only do I want to fully control the image that will be displaying; but also when it will be displaying. To achieve this behavior, I’ll be hosting a web page with the URL of the desired image. I can change the image URL whenever I want or echo an empty string to avoid a change on the wallpaper.

For instance, hosted page would look as follows:

PHP
<?php
    echo "http://www.somesite.com/someimage.jpg";
?> 

Now, to retrieve the image, I’ll be using C# HttpWebRequest to ping the hosted PHP, and retrieve the image URL in an HttpWebResponse. In addition to retrieving the image URL, I will also have the application send the host’s computer name to the PHP as POST data. That way, I can store each of the soliciting computer’s names on a database and personalize the image URL for each computer/person.

The code to send the HttpRequest and get the HttpResponse would be:

C#
/// <summary>
/// Gets the image URL with an HTTPRequest
/// </summary>
/// <param name="Uri">Site URL send the httprequest</param>
/// <param name="RequestItems">NameValueCollection with the data to send as post</param>
/// <returns>Image Url</returns>
static string getImageUrl(string Uri, NameValueCollection Items)
{
    HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(Uri);
    webrequest.KeepAlive = false;
    webrequest.Method = "POST";
    webrequest.ContentType = "application/x-www-form-urlencoded";
    string _Request = BuildRequestBody(Items);
    webrequest.ContentLength = _Request.Length;
    using (Stream writeStream = webrequest.GetRequestStream())
    {
        byte[] bytes = Encoding.ASCII.GetBytes(_Request);
        writeStream.Write(bytes, 0, bytes.Length);
        writeStream.Close();
    }

    HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();            
    StreamReader responseStream = new StreamReader
                                  (webresponse.GetResponseStream(), Encoding.ASCII);
    string _Response = responseStream.ReadToEnd();
    responseStream.Close();
    webresponse.Close();

    return _Response;
} 

Downloading the Image

Once we have the image URL, we need to download it and store it on a temporal folder (you can change this folder to whatever you want). I will also change the image format and save it as a bitmap (BMP). With the method I’ll be using Windows 7 and newer versions support both BMPs and JPGs file formats for wallpapers. But to achieve compatibility with Windows XP system, I’m saving the image as a bitmap regardless of the system the application is running.

C#
_imagePath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");

/// <summary>
/// Downloads an image and saves it to a temp path
/// </summary>
/// <param name="url">Uri url</param>
private static void DownloadImage(Uri url)
{
   Stream s = new System.Net.WebClient().OpenRead(url.ToString());
   Image img = Image.FromStream(s);           
   img.Save(_imagePath, System.Drawing.Imaging.ImageFormat.Bmp);
} 

Changing the Wallpaper

Changing the wallpaper is not accessible through managed code; so I’ll be calling SystemParametersInfo from the Win32 API, as proposed by Sean Campbell, which is hosted on user32.dll. This function retrieves or sets the value of one of the system-wide parameters and updates the user profile. (Detailed information on the function can be found here.

The function SystemParametersInfo accepts four arguments:

  • The system-wide parameter to be retrieved or set. In this case, I’m using SPI_SETDESKWALLPAPER.
  • Two parameters whose usage and format depends on the system parameter being queried or set, these parameters usually represent the data to be set with the operation that is taking place. In this case, I’m setting one as the path of the new image, and the other, since it has no use, is set up to 0.
  • The final parameter is the broadcasting options if a system parameter is being set.
    C#
    const int SPI_SETDESKWALLPAPER = 20;
    const int SPIF_UPDATEINIFILE = 0x01;
    const int SPIF_SENDWININICHANGE = 0x02;
    
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern int SystemParametersInfo
           (int uAction, int uParam, string lpvParam, int fuWinIni);
    
    SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, imagePath, 
           SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);  

Now, for setting the style of the wallpaper, there is a registry entry under HKEY_CURRENT_USER\Control Panel\Desktop where we will add the following keys:

  1. WallpaperStyle: Determines how the desktop bitmap is displayed on the desktop if the bitmap is not tiled.
    • Value 0 to center the bitmap on the desktop
    • Value 2 to stretch the bitmap vertically and horizontally to fit the desktop
  2. TileWallpaper:
    • Value 1 to display the wallpaper bitmap as repeating tiles
    • Value 0 to display the bitmap normally

You can find more information about this registry keys at this link.

As suggested by Sean Campbell, in his article, I will also create an Enum to hold the possible values for the wallpaper style and have a switch to properly set the values on the registry key, depending on the style selected:

C#
case Style.Tiled:
    key.SetValue(@"WallpaperStyle", "1");
    key.SetValue(@"TileWallpaper", "1");
    break;
case Style.Centered:
    key.SetValue(@"WallpaperStyle", "1");
    key.SetValue(@"TileWallpaper", "0");
    break;
case Style.Stretched:
    key.SetValue(@"WallpaperStyle", "2");
    key.SetValue(@"TileWallpaper", "0");
    break;
case Style.Fit:
    key.SetValue(@"WallpaperStyle", "6");
    key.SetValue(@"TileWallpaper", "0");
    break;
case Style.Fill:
    key.SetValue(@"WallpaperStyle", "10");
    key.SetValue(@"TileWallpaper", "0");
    break;  

A fully detailed article Setting Wallpaper by Sean Campbell can be found at this link.

Adding a Log on the Host Machine

Since the application will be running in the host machine (the unlocked computer), we won’t know if the application unexpectedly crashes, nor the reasons of the crash. So I’m adding a small .txt log file in which to store the reported exceptions, stack trace and time of the event. So if we suspect that something is not going as expected, we can later check the log file. In addition, we could also send the latest entries of the log as a web request, catch them and store them in a database. The procedure to achieve this would be the same as the procedure explained above for retrieving the image URL and sending the computer’s name. This feature is out of the scope of this article but I just wanted to write down the idea so readers can implement it.

In order to write to the log file, I created a log Logger class which uses the StreamWriter class. The code for adding a log message is as follows:

C#
/// <summary>
/// Adds a message entry to the log file
/// </summary>
/// <param name="message">message</param>
public void LogMessage(string message)
{
    using (sw = new StreamWriter(FilePath, true))
    {
        sw.WriteLine(string.Format("{0}\t{1}", DateTime.Now.ToString(),message));
        sw.Close();
    }
} 

Copying the Application and Scheduling

So far, we have written an application that will send an http request with the host’s name to our PHP web page and retrieve an image URL. Then the image will be downloaded, stored on a temporal folder, converted to a bitmap and finally the application will set that image as the current wallpaper on the host’s machine.

So far so good. But two things are still missing:

  • Copying the application to the host machine
  • Adding retry/loop logic in order to change the wallpaper periodically

To copy the file is no issue at all. You can manually do it, or we can automate it with a call to File.Copy method. But things are not that simple concerning the retry/loop logic.

For the retry/loop logic; after researching the web for the best approaches to achieve this, I found the following solutions:

  • Windows service: Implement a Windows service with the same code as the application.
  • Have the application running on the background looping and waiting: Modify the actual application to keep it always running on the background.
  • Windows task scheduler: Use windows task scheduler to trigger the launch of our application.

Let’s discuss each of the following options. First, we have Windows services. This is probably the most efficient and reliable approach. Unfortunately for our purposes, changing the desktop wallpaper cannot be done through a Windows service. Changing the desktop wallpaper depends on the user that is currently logged. It is a user specific task; whereas services run in their own session. Services run on an isolated and independent session from any of the users’ sessions, for security reasons. So, since we want to modify a user’s session wallpaper, doing this through Windows services is unfeasible.

Since changing the wallpaper is a user specific task, our only option so far is to have an application run the code within the user’s session. As second approach, we could modify our coded application adding a thread logic that will loop forever, changing the wallpaper and sleeping for some minutes. A second approach following the same loop/retry logic would be the use of C# Timer ElapsedEventHandler. The result would be the creation of a timer that will repeat each X minutes and call the change wallpaper function each time the event is launched:

C#
Timer imageChangeTimer = new Timer(5000);
imageChangeTimer.Elapsed += new ElapsedEventHandler(imageChangeTimer_Elapsed);
imageChangeTimer.Start(); 

Both approaches work, but they would need to have the application alive and running on the background forever. According to some forums, this approach is far from desirable. Having an application always running in the background to execute only an action every period of time will only consume the computer’s resources even when our application is on idle periods.

Finally, the best approach for our needs, and ironically the simplest and easiest to implement is to use Windows Task Scheduler. We can easily have our application run every X minutes by scheduling a task with Windows Task Scheduler. I’ll be using Schtasks.exe (Task Scheduler command line) to schedule a run of the application every 5 minutes. The full documentation and options of Schtasks.exe can be found here.

To summarize what we have just discussed, we have just seen that to periodically change the desktop wallpaper, the best approach is to use Windows Task Scheduler. So, I’ll be creating a second application that will achieve the last two missing points we have discussed. This second application will copy our first application to a “save folder” on the host machine and schedule a Windows task for our recently copied executable.

Using C# Diagnostics.Process class, we can easily automate the scheduling of the task as follows:

C#
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = @"schtasks";
p.StartInfo.Arguments = string.Format("/create /tn {0} /tr {1} /sc MINUTE /mo 5", 
        "\"AppVshost32\"", path);
p.Start();
p.WaitForExit();

Final Touches

I have written the application as a console application. As a final touch, in order to hide the application and raise no suspicion on the user that a new application has been launched, I’ll hide the console window by changing the “Output type” of my Visual Studio project to “Windows Application”. You can change this property under Project -> Properties -> Application in Visual Studio.

Finally, after a long trail, we have come up with two applications. Our main application that changes the user’s wallpaper and a second application, let us call it our installer, which automates the process of copying the main application to a secure folder and sets up Windows Task Scheduler to periodically launch the application on the background.

Conclusions

This article showed the integration of multiple technologies and approaches to achieve a programming prank. Nonetheless, the methods and approaches discussed here regarding the repetition and automation of the application have more potential to offer, maybe under a different context (not a prank). I hope that more than the code exposed on this article, the ideas, options and alternatives exposed here prove to be useful to the reader and enlighten the perspective about automation and its full potential on daily activities.

References and Important Links

Besides the mentioned links in the body of the article. I would like to mention the following articles which were useful to me while researching the topic:

Articles to set the wallpaper using C#:

History

  • 3rd December, 2013: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAll that's missing.... Pin
2374118-Sep-15 9:33
2374118-Sep-15 9:33 
GeneralMy vote of 4 Pin
webmaster44223-Apr-15 2:13
webmaster44223-Apr-15 2:13 
GeneralMy vote of 1 Pin
pt14013-Dec-13 10:37
pt14013-Dec-13 10:37 
SuggestionRe: My vote of 1 PinPopular
John B Oliver11-Dec-13 10:43
John B Oliver11-Dec-13 10:43 
GeneralRe: My vote of 1 Pin
Marco Bertschi12-Dec-13 1:31
protectorMarco Bertschi12-Dec-13 1:31 
Alright, there are valid use cases where a program needs to be able to change the wallpaper, for example if a company wants to dynamically apply a wallpaper for all employee computers.
Even though the different ways of setting the wallpapers are demonstrated for a purpose which may seem childish to some people, it remains enough technologically valuable content in the article to report you (and your not really valuable comment) as abusive.
This is a developer website, and the article votes are intended to judge the technical value an article delivers.
Veni, vidi, caecus

GeneralRe: My vote of 1 Pin
pt140112-Dec-13 2:32
pt140112-Dec-13 2:32 

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.