Changing the Desktop Wallpaper
The .NET framework does not natively support changing the desktop wallpaper. However, it is possible to set the wallpaper image using Platform Invocation Services (P/Invoke) by calling the SystemParametersInfo function.
The desktop wallpaper is the background image for Microsoft Windows that is visible behind any open windows. All versions of Windows that support the .NET framework allow a static desktop wallpaper to be applied but not all allow the image to be changed automatically.
In this article we will create a C# class that sets the desktop wallpaper image. You could use this as the basis for a program that cycles through multiple wallpapers with regular changes. Unfortunately, the .NET framework does not natively support setting the background image so we need to use a Windows API function.
Referencing the API
As we will be using a Windows API function we will need to use Platform Invocation Services (P/Invoke). The attributes that we need are in the System.Runtime.InteropServices namespace so add the following using directive so simplify the code:
The Windows API function that we will use to change the wallpaper is SystemParametersInfo and is found in the user32 API. To provide access to it, create a class named "WallpaperSetter" and add the following declaration to the class:
private static extern bool SystemParametersInfo(
uint uiAction, uint uiParam, string pvParam, uint fWinIni);
SystemParametersInfo can be used to retrieve or change a number of operating system settings. To determine which item is being configured, you pass an integer value to the uiAction parameter. To change the wallpaper, the value is defined in a constant named, "SPI_SETDESKTOPWALLPAPER". The uiParam parameter is unused when changing the wallpaper and so should be set to zero. The pvParam parameter is used. We will pass the file path and name into it.
The final argument, fWinIni, determines how the change is written to user profile and whether a message should be sent to other windows to notify them of the update. We will be updating the profile using the SPIF_UPDATEINIFILE constant. To define the two constants, add the following to the class:
const uint SPI_SETDESKWALLPAPER = 0x14;
const uint SPIF_UPDATEINIFILE = 0x01;
NB: The function returns a Boolean value. True indicates success and false means that an error occurred during the process. You should check the value and respond appropriately.
To complete the WallpaperSetter class we will add a method that changes the wallpaper. The method below accepts the path to the desired image in its only parameter. It uses this path, along with the two constants, to call the API function. To see the method in action, download the demo project using the link at the top of the page. This is a simple Windows Forms application that allows you to browse for a wallpaper and apply it to the desktop.
public void SetWallpaper(string path)
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE);
3 February 2011