Search code examples
c#desktop

Change desktop background in C#


I try to change the background using C#.Example:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern Int32 SystemParametersInfo(UInt32 uiAction, UInt32
    uiParam, String pvParam, UInt32 fWinIni);
    private static UInt32 SPI_SETDESKWALLPAPER = 20;
    private static UInt32 SPIF_UPDATEINIFILE = 0x1;

And then

  SystemParametersInfo(SPI_SETDESKWALLPAPER, 1, @"C:\background.bmp", SPIF_UPDATEINIFILE);
                            }

But it doesn't work...Help?


Solution

  • pvParam should be a local file. It will not work for urls...

    First download the image, then give its local path to SystemParametersInfo method.

    var filename = "4.jpg";
    new WebClient().DownloadFile("http://www.scottgames.com/4.jpg", filename);
    SystemParametersInfo(SPI_SETDESKWALLPAPER, 1, filename, SPIF_UPDATEINIFILE);