I'm making a simple program to change my computer background. I found a stackoverflow question online that more or less covered what I wanted to do. I can now successfully change my wallpaper to be tiled, centered, and stretched from an online image URL. However, in control panel, there are options to have the wallpaper in the positions "fit" and "fill". How can I set the wallpaper in fit/fill mode programmatically?
Relevant code:
public enum Style : int
{
Tiled,
Centered,
Stretched
}
public class Wallpaper
{
public 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 void Set(string URL, Style style)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = httpWebReponse.GetResponseStream();
System.Drawing.Image img = Image.FromStream(stream);
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);
}
}
Are the keys for fit/fill unavailable? I searched online for a while, and only found the tiled, centered, and stretched.
This might help you.
Excerpt from Set the desktop wallpaper
case WallpaperStyle.Fit: // (Windows 7 and later)
key.SetValue(@"WallpaperStyle", "6");
key.SetValue(@"TileWallpaper", "0");
break;
case WallpaperStyle.Fill: // (Windows 7 and later)
key.SetValue(@"WallpaperStyle", "10");
key.SetValue(@"TileWallpaper", "0");
break;
I belive you can easily adapt that to your code.