Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change my Windows desktop wallpaper programmatically?

I'd wish to set a wallpaper for Windows XP using C#. I've developed the code so it perfectly works in Windows 7, but apparently it's not the same for XP. I add that wallpaper as a resource, set its compile action as Content and Always copy. It, curiously, sets the correct Wallpaper name inside the Desktop's properties dialog. However, the wallpaper is not set. My code looks like this:

public sealed class Wallpaper
{
    Wallpaper() { }

    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);

    public enum Style : int
    {
        Tiled,
        Centered,
        Stretched
    }

    public static void Set(string wpaper, Style style)
    {
        RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
        if (style == Style.Stretched)
        {
            key.SetValue(@"WallpaperStyle", 2.ToString());
            key.SetValue(@"TileWallpaper", 0.ToString());
        }

        if (style == Style.Centered)
        {
            key.SetValue(@"WallpaperStyle", 1.ToString());
            key.SetValue(@"TileWallpaper", 0.ToString());
        }

        if (style == Style.Tiled)
        {
            key.SetValue(@"WallpaperStyle", 1.ToString());
            key.SetValue(@"TileWallpaper", 1.ToString());
        }

        string tempPath = "Resources\\"+wpaper;
        SystemParametersInfo(SPI_SETDESKWALLPAPER,
            0,
            tempPath,
            SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
    }
}

When calling Wallpaper.Set("wpapername"), it gets the wallpaper from project resources. It works on Win7, but not on WinXP. Am I doing something wrong?

like image 393
Sergi Juanola Avatar asked Dec 07 '11 11:12

Sergi Juanola


People also ask

How do I change my background in CMD?

To set the default Command Prompt window color, select the upper-left corner of the Command Prompt window, select Defaults, select the Colors tab, and then select the colors that you want to use for the Screen Text and Screen Background.

How do I force a wallpaper change?

Click Start, click Run, and then type gpedit. msc. Under Local Computer Policy, expand User Configuration, expand Administrative Templates, expand Desktop, and then click Active Desktop. Double-click Active Desktop Wallpaper.

How do I change my wallpaper if I haven't activated Windows?

Here's how to do it. Press Win + E to open File Explorer. Open the folder with the image that you want to set as wallpaper. Right-click on your preferred image and select Set as desktop background.


1 Answers

Well, this is a bit awkward, but I'll answer my own question with what I found.

I had to reuse more code from the accepted answer here. Basically the problem in XP was that it needed to use a bmp file, so I managed to convert a project resource to a bmp file using that previous example and a little of tweaking. The Set method works perfectly this way:

public static void Set(string wpaper, Style style)
{
    using(System.Drawing.Image img = System.Drawing.Image.FromFile(Path.GetFullPath(wpaper)))
    {
        string tempPath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");

        img.Save(tempPath, System.Drawing.Imaging.ImageFormat.Bmp);

    }

    RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);

    if (style == Style.Stretched)
    {
        key.SetValue(@"WallpaperStyle", 2.ToString());

        key.SetValue(@"TileWallpaper", 0.ToString());

    }

    if (style == Style.Centered)
    {
        key.SetValue(@"WallpaperStyle", 1.ToString());

        key.SetValue(@"TileWallpaper", 0.ToString());

    }

    if (style == Style.Tiled)
    {
        key.SetValue(@"WallpaperStyle", 1.ToString());

        key.SetValue(@"TileWallpaper", 1.ToString());

    }

    SystemParametersInfo(SPI_SETDESKWALLPAPER,
        0,
        tempPath,
        SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);

}

The important part is on the third line of this code (System.Drawing.Image.FromFile(Path.GetFullPath(wpaper));).

like image 125
Sergi Juanola Avatar answered Sep 20 '22 12:09

Sergi Juanola