How can I change the desktop wallpaper using C# Code?
Let me fix it myself msc. Under Local Computer Policy, expand User Configuration, expand Administrative Templates, expand Desktop, and then click Active Desktop. Double-click Active Desktop Wallpaper. On the Setting tab, click Enabled, type the path to the desktop wallpaper that you want to use, and then click OK.
To do this, right-click the image you want to set as your wallpaper, and then select Set as Background from the drop-down menu. If you are sure that you want to set the image as the background, click Yes.
Here's a class yanked from an app I wrote a year or two ago:
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(Uri uri, Style style) { System.IO.Stream s = new System.Net.WebClient().OpenRead(uri.ToString()); System.Drawing.Image img = System.Drawing.Image.FromStream(s); 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); } }
I haven't tested it extensively, so use at your own risk.
Bases on this useful answer, I've also made my own app to set wallpaper matching screen resolution.
But the registry settings were wrong. Here are the correct values (tested on Win 7, Win 8.1, Win 10).
if (style == Style.Fill) { key.SetValue(@"WallpaperStyle", 10.ToString()); key.SetValue(@"TileWallpaper", 0.ToString()); } if (style == Style.Fit) { key.SetValue(@"WallpaperStyle", 6.ToString()); key.SetValue(@"TileWallpaper", 0.ToString()); } if (style == Style.Span) // Windows 8 or newer only! { key.SetValue(@"WallpaperStyle", 22.ToString()); key.SetValue(@"TileWallpaper", 0.ToString()); } if (style == Style.Stretch) { key.SetValue(@"WallpaperStyle", 2.ToString()); key.SetValue(@"TileWallpaper", 0.ToString()); } if (style == Style.Tile) { key.SetValue(@"WallpaperStyle", 0.ToString()); key.SetValue(@"TileWallpaper", 1.ToString()); } if (style == Style.Center) { key.SetValue(@"WallpaperStyle", 0.ToString()); key.SetValue(@"TileWallpaper", 0.ToString()); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With