Set the Windows desktop picture in C#

[desktop picture]

This example sets the Windows desktop picture much as you can manually in Windows 10. (I can’t guarantee that it will work without any changes in other versions of Windows.)

Exactly how you set the desktop picture manually depends on your version of Windows. In Windows 10, follow these steps.

  • Right-click the desktop and select Personalize
  • Open the Background dropdown and select Picture
  • Choose a picture either from the list or by browsing
  • Choose a Fit style

The Fit styles are:

  • Fill – Scales the picture evenly until it fills the desktop. Parts of the picture may extend off of the desktop.
  • Fit – Scales the picture evenly until it is as big as possible while completely fitting on the desktop.
  • Stretch – Stretches the picture so it fills the desktop. The image may be distorted.
  • Tile – The picture is placed in the desktop’s upper left corner and then repeated to fill the desktop.
  • Center – The picture is placed at its original size centered on the desktop.
  • Span – This makes the picture span all of the system’s monitors. If you have only one monitor, it works much like Fill, although the picture is centered slightly differently.

Note that the program doesn’t seem to work if the system’s desktop settings are in certain states. If the program doesn’t work, try setting the desktop picture manually and then run the program. That is necessary in earlier versions of windows and seems to be sometimes necessary in Windows 10. After you set the desktop picture manually, the program seems to work fine.

The following DisplayPicture method sets the desktop’s picture.

// Display the file on the desktop.
private void DisplayPicture(string file_name, bool update_registry)
{
    try
    {
        // If we should update the registry,
        // set the appropriate flags.
        uint flags = 0;
        if (update_registry)
            flags = SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE;

        // Set the desktop background to this file.
        if (!SystemParametersInfo(SPI_SETDESKWALLPAPER,
            0, file_name, flags))
        {
            MessageBox.Show("SystemParametersInfo failed.",
                "Error", MessageBoxButtons.OK,
                MessageBoxIcon.Exclamation);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error displaying picture " +
            file_name + ".\n" + ex.Message,
            "Error", MessageBoxButtons.OK,
            MessageBoxIcon.Exclamation);
    }
}

The program uses the SystemParametersInfo API function to set the desktop picture.

The final parameter to SystemParametersInfo indicates whether the function should modify the Registry to make the new picture permanent. If the user checks the Update Registry checkbox, the program uses SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE for the final parameter so the change is permanent and you will see the new image if you log out or reboot.

The method sets the flags variable to indicate whether the function should update the Registry and then calls the function to display the new desktop picture.

The documentation for SystemParametersInfo says the method returns true unless it fails, for example if the file doesn’t exist. In my tests the method seems to return true even if the file doesn’t exist.

Notes:

In some previous versions of Windows, the SystemParametersInfo function could only display BMP and JPG files. In earlier versions of this example, the program loaded other types of files (such as GIF files), saved them into a temporary BMP file, and then displayed the temporary file.

In some previous versions of Windows, the SystemParametersInfo function also only worked if the system was already displaying desktop pictures. If you’re using one of those versions, manually make Windows display a picture and then the program should work. You could probably use code to switch Windows to picture display mode, but this example is intended to change the desktop picture not to reconfigure Windows.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in API, graphics, interoperability, system and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *