private static void TheScientist() { PictureDataItem picDataItem = RssReader.GetPictureData(new TheScientistSource()); //Console.WriteLine(pictureUri.AbsoluteUri); string outputPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); outputPath = Path.Combine(outputPath, "Wallpapers"); if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } Wallpaper.Set(picDataItem, Wallpaper.Style.Stretched, outputPath); }
public static void Set(PictureDataItem picDataItem, Style style, string outputPath) { string savedImagePath = DownloadImageFromUri(picDataItem.Uri, outputPath, picDataItem.Title); using (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()); } int res = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, savedImagePath.Replace('\\', '/'), SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE); if (res != 1) //TRUE { var err = Marshal.GetLastWin32Error(); throw new Win32Exception(err, string.Format("SystemParametersInfo() failed, resulting: {0}.", res)); } //updating reg key to be the correct path to file key.SetValue("Wallpaper", savedImagePath, RegistryValueKind.String); } }