1

I changed desktop wallpaper with this code:

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 static void Set(CommandRemoteOsSettings remoteOsSettings)
    {
       CommandRemoteOsSettings;
        List commandsList = remoteOsSettings.getCommandsList();
        RemotoSetBackground cmdSetBackground = (RemotoSetBackground)commandsList.get(0);
        string fit = cmdSetBackground.getFit();
        string aaaa = cmdSetBackground.getImage();
        byte[] bytes = Convert.FromBase64String(cmdSetBackground.getImage());
        System.Drawing.Image image;
        string tempPath = string.Format("{0}\\USC\\temp\\tempbg.bmp", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
        System.IO.FileInfo file = new System.IO.FileInfo(tempPath);
        file.Directory.Create();
        using (MemoryStream ms = new MemoryStream(bytes))
        {
            using (image = Image.FromStream(ms))
            {
                image.Save(tempPath);
            }
        }
        RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
        if (fit.Equals("Stretched"))
        {
            key.SetValue(@"WallpaperStyle", 2.ToString());
            key.SetValue(@"TileWallpaper", 0.ToString());
        }

        else if (fit.Equals("Centered"))
        {
            key.SetValue(@"WallpaperStyle", 1.ToString());
            key.SetValue(@"TileWallpaper", 0.ToString());
        }

        if (fit.Equals("Tiled"))
        {
            key.SetValue(@"WallpaperStyle", 1.ToString());
            key.SetValue(@"TileWallpaper", 1.ToString());
        }
        SystemParametersInfo(SPI_SETDESKWALLPAPER,
         0, tempPath, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);  }
    }
}

the problem is, after I restart the computer the desktop wallpaper image returns to the image that was before my change. thanks!

Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
Achinoam
  • 11
  • 1
  • Have you checked to see if something else out there is changing it back? You can confirm this by changing the wallpaper using Windows desktop settings – JayV Jun 18 '18 at 09:00
  • Can be marked to duplicate to this [question](https://stackoverflow.com/questions/1061678/change-desktop-wallpaper-using-code-in-net) – Elbek Jun 18 '18 at 09:01
  • @Elbek it's not the same question. This one is about the changes not being kept after restarting. – Isma Jun 18 '18 at 09:02
  • when I change it via settings the wallpaper stays after restart – Achinoam Jun 18 '18 at 09:08

0 Answers0