Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change desktop wallpaper using code in .NET

How can I change the desktop wallpaper using C# Code?

like image 224
Sauron Avatar asked Jun 30 '09 04:06

Sauron


People also ask

How do I force my desktop background to change?

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.

How do you set an image in a web page as Desktop Wallpaper?

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.


2 Answers

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.

like image 154
Neil N Avatar answered Sep 28 '22 02:09

Neil N


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()); } 
like image 32
badsamaritan Avatar answered Sep 28 '22 03:09

badsamaritan