Changing the windows desktop background image

Hi,

I want to write a program that automatically changes the windows desktop bakckground image using something like an image playlist. Is it possible to change the desktop background from a C# program, and if so how would I do that

Greetings,
El Loco



Answer this question

Changing the windows desktop background image

  • abc_acb

    I'm not aware of managed methods to do so, but you can do so by calling one single unmanaged API: SystemParametersInfo from user32.dll. You must define the following signature:

    [DllImport("user32.dll", CharSet=Charset.Auto, SetLastError=true)]
    private static extern int SystemParametersInfo (int uAction, int uParam, string szImagePath, int fuWinIni);

    A minimal implementation would be:

    public bool ChangeWallpaper (string path) {
    const int SPI_SETDESKWALLPAPER = 0x14; // constant code, from WinUser.h

    const int SPIF_UPDATEINIFILE = 0x01; // will make your change permanent in the user profile
    const int SPIF_SENDCHANGE = 0x02; // will inform all the windows that something has changed

    int result = SystemParametersInfo (SPI_DESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE| SPIF_SENDCHANGE);
    return (result != 0);
    }

    (warning: untested code)

    HTH
    --mc


  • Changing the windows desktop background image