コード例 #1
0
        /// <summary>
        ///     Restore wallpaper back to its original image
        /// </summary>
        public void Restore()
        {
            try
            {
                using (var key = Registry.CurrentUser.OpenSubKey(Wallpaper.KeyNames.WallpaperParentKey, true))
                {
                    if (key == null)
                    {
                        return;
                    }

                    //restore reg keys for style and tiling
                    key.SetValue(Wallpaper.KeyNames.WallpaperStyle,
                                 backupWallKey.Values[Wallpaper.KeyNames.WallpaperStyle]);
                    key.SetValue(Wallpaper.KeyNames.TileWallpaper,
                                 backupWallKey.Values[Wallpaper.KeyNames.TileWallpaper]);

                    //restore wallpaper image
                    SysCall.SetSystemWallpaper(backupWallKey.Values[Wallpaper.KeyNames.WallpaperPath]);
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
            }

            Logger.Info("wallpaper restored");
        }
コード例 #2
0
ファイル: Wallpaper.cs プロジェクト: jaminbenj4/ClassyPhotos
        //private static readonly string ClassyPath = $"\\Roaming{WindowsThemesPath}\\{ClassyFilename}";


        /*
         *      public static bool PaintWall(string wallFilePath, Style style)
         *      {
         *          Image img = null;
         *          try
         *          {
         *              img = Image.FromFile(Path.GetFullPath(wallFilePath));
         *          }
         *          catch (Exception e1)
         *          {
         *              Console.WriteLine(e1);
         *          }
         *
         *          return PaintWall(img, style);
         *      }
         */

        public static void PaintWall(Image image, Style style)
        {
            var primaryFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var wallFilePath  = Path.Combine(primaryFolder + WindowsThemesPath, ClassyFilename);

            try
            {
                var imgTemp = new Bitmap(image);
                imgTemp.Save(wallFilePath, ImageFormat.Bmp);
                Logger.Info("Wallpaper saved to primary path: " + wallFilePath);
            }
            catch (Exception e1)
            {
                Logger.Error(e1);
                return;
            }

            try
            {
                var key = Registry.CurrentUser.OpenSubKey(KeyNames.WallpaperParentKey, true);
                if (key == null)
                {
                    throw new NullReferenceException(string.Format("Registry key [{0}] does not exist",
                                                                   KeyNames.WallpaperParentKey));
                }
                if (style == Style.Fill)
                {
                    key.SetValue(KeyNames.WallpaperStyle, 10.ToString());
                    key.SetValue(KeyNames.TileWallpaper, 0.ToString());
                }
                if (style == Style.Fit)
                {
                    key.SetValue(KeyNames.WallpaperStyle, 6.ToString());
                    key.SetValue(KeyNames.TileWallpaper, 0.ToString());
                }
                if (style == Style.Span) // Windows 8 or newer only!
                {
                    key.SetValue(KeyNames.WallpaperStyle, 22.ToString());
                    key.SetValue(KeyNames.TileWallpaper, 0.ToString());
                }
                if (style == Style.Stretch)
                {
                    key.SetValue(KeyNames.WallpaperStyle, 2.ToString());
                    key.SetValue(KeyNames.TileWallpaper, 0.ToString());
                }
                if (style == Style.Tile)
                {
                    key.SetValue(KeyNames.WallpaperStyle, 0.ToString());
                    key.SetValue(KeyNames.TileWallpaper, 1.ToString());
                }
                if (style == Style.Center)
                {
                    key.SetValue(KeyNames.WallpaperStyle, 0.ToString());
                    key.SetValue(KeyNames.TileWallpaper, 0.ToString());
                }

                SysCall.SetSystemWallpaper(wallFilePath);

                Logger.Info("wallpaper set to " + wallFilePath);
            }
            catch (Exception e)
            {
                Logger.Error(e);
            }
        }