コード例 #1
0
        /// <summary>
        /// Set the Desktop background to the supplied image.  Image must be
        /// saved as BMP (saves to My Pictures), and uses the specified
        /// style (Tiled, Centered, Stretched).
        /// </summary>
        ///
        /// <param name="img">The Image file that will be used to set
        /// the background</param>
        ///
        /// <param name="style">The style that was deturmend in another
        /// function</param>
        internal static void SetDesktopBackground(Bitmap img,
                                                  DesktopBackgroundStyle style)
        {
            string picturesPath = Path.Combine(ApplicationInfo.localDataPath, "backroundcycler.bmp");

            // Clear the current background (releases lock on current bitmap)
            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0,
                                 string.Empty, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);

            try
            {
                // Convert to BMP and save
                img.Save(picturesPath, System.Drawing.Imaging.ImageFormat.Bmp);
            }
            catch (ExternalException)
            {
                return;
            }

            RegistryKey key = Registry.CurrentUser.OpenSubKey(
                @"Control Panel\Desktop", true);

            switch (style)
            {
            case DesktopBackgroundStyle.Stretched:
                key.SetValue(@"WallpaperStyle", "2");
                key.SetValue(@"TileWallpaper", "0");
                break;

            case DesktopBackgroundStyle.Centered:
                key.SetValue(@"WallpaperStyle", "0");
                key.SetValue(@"TileWallpaper", "0");
                break;

            case DesktopBackgroundStyle.Tiled:
                key.SetValue(@"WallpaperStyle", "1");
                key.SetValue(@"TileWallpaper", "1");
                break;

            case DesktopBackgroundStyle.Nothing:
                throw new ArgumentException(
                          "nothing cannot be used in SetDesktopBackground() Style", "Style");

            case DesktopBackgroundStyle.Scaled:
                throw new ArgumentException(
                          "Scaled cannot be used in SetDesktopBackground() Style", "Style");
            }

            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0,
                                 picturesPath,
                                 SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
        }
コード例 #2
0
        /// <summary>
        /// Sets the desktop background.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <param name="desktopBackgroundStyle">The desktop background style.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">Could not get assembly location.</exception>
        public static bool SetDesktopBackground(string file, DesktopBackgroundStyle desktopBackgroundStyle)
        {
            try
            {
                using (var bitmap = new Bitmap(file))
                {
                    var directoryName = Path.GetDirectoryName(file);
                    if (!string.IsNullOrWhiteSpace(directoryName) && Directory.Exists(directoryName))
                    {
                        var desktopBackgroundPath = Path.Combine(directoryName, $"{Path.GetFileNameWithoutExtension(file)}.bmp");
                        Logger.Info($"Creating bitmap {desktopBackgroundPath} to set as background");

                        bitmap.Save(desktopBackgroundPath, ImageFormat.Bmp);

                        using (var registryKey = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true))
                        {
                            if (registryKey != null)
                            {
                                switch (desktopBackgroundStyle)
                                {
                                    case DesktopBackgroundStyle.Filled:
                                        registryKey.SetValue(@"WallpaperStyle", "10");
                                        registryKey.SetValue(@"TileWallpaper", "0");
                                        break;
                                    case DesktopBackgroundStyle.Stretched:
                                        registryKey.SetValue(@"WallpaperStyle", "2");
                                        registryKey.SetValue(@"TileWallpaper", "0");
                                        break;
                                    case DesktopBackgroundStyle.Centered:
                                        registryKey.SetValue(@"WallpaperStyle", "0");
                                        registryKey.SetValue(@"TileWallpaper", "0");
                                        break;
                                    case DesktopBackgroundStyle.Tiled:
                                        registryKey.SetValue(@"WallpaperStyle", "0");
                                        registryKey.SetValue(@"TileWallpaper", "1");
                                        break;
                                }
                            }
                            SystemParametersInfo(SpiSetDeskWallpaper, 0, desktopBackgroundPath, SpifUpdateIniFile | SpifSendWinIniChange);
                            Logger.Info($"Image {desktopBackgroundPath} has successfully been set to the background.");
                            return true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "An exception occurred setting the desktop background.");
            }
            return false;
        }
コード例 #3
0
        /// <summary>
        /// Return a DesktopBackgroundStyle enum based on image size and
        /// behavior settings.
        /// </summary>
        ///
        /// <param name="img">the image that we need to get the
        /// size from</param>
        ///
        /// <returns>One of the values in DesktopBackgroundStyle enum</returns>
        private DesktopBackgroundStyle DetermineImageStyle(Bitmap img)
        {
            int smallestSize =
                int.Parse(settings.SmallestImageSize.ToString());
            int imgW = img.Width, imgH = img.Height;
            int desktopW = Screen.PrimaryScreen.Bounds.Width;
            int desktopH = Screen.PrimaryScreen.Bounds.Height;

            string style;

            if (imgW > desktopW || imgH > desktopH)
            {
                style = settings.LargeImageBehavior;
            }
            else if (imgW <= smallestSize || imgH <= smallestSize)
            {
                style = settings.SmallestImageBehavior;
            }
            else
            {
                style = settings.SmallImageBehavior;
            }

            try
            {
                return((DesktopBackgroundStyle)Enum.Parse(
                           typeof(DesktopBackgroundStyle),
                           style, true));
            }
            catch
            {
                // ok the user typed in something in one of the DesktopBackgroundStyle
                // comboboxes so now we have a long dialog box call and return center
                MessageBox.Show("ERROR with setting background style\r\n" +
                                "Could be because you typed something other than" +
                                " Tiled, Centered, Stretched, Scaled, Nothing\r\n" +
                                "Defaulting to Centered",
                                "Background Style Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                                MessageBoxOptions.ServiceNotification, false);
                return(DesktopBackgroundStyle.Centered);
            }
        }
コード例 #4
0
        /// <summary>
        /// Set the Desktop background to the supplied image.  Image must be
        /// saved as BMP (saves to My Pictures), and uses the specified
        /// style (Tiled, Centered, Stretched).
        /// </summary>
        /// <param name="img"></param>
        /// <param name="style"></param>
        public static void SetDesktopBackground(Bitmap img, DesktopBackgroundStyle style)
        {
            string picturesPath = Path.Combine(
                System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),
                "coding4fun-desktop.bmp");

            // Clear the current background (releases lock on current bitmap)
            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0,
                                 "",
                                 SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);

            // Convert to BMP and save)
            img.Save(picturesPath,
                     System.Drawing.Imaging.ImageFormat.Bmp);

            //Write style info to registry
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);

            switch (style)
            {
            case DesktopBackgroundStyle.Stretched:
                key.SetValue(@"WallpaperStyle", "2");
                key.SetValue(@"TileWallpaper", "0");
                break;

            case DesktopBackgroundStyle.Centered:
                key.SetValue(@"WallpaperStyle", "1");
                key.SetValue(@"TileWallpaper", "0");
                break;

            case DesktopBackgroundStyle.Tiled:
                key.SetValue(@"WallpaperStyle", "1");
                key.SetValue(@"TileWallpaper", "1");
                break;
            }

            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0,
                                 picturesPath,
                                 SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
        }
コード例 #5
0
        /// <summary>
        /// Grab list of files, filter out bad choices,
        /// return one randomly good file
        /// </summary>
        /// <returns>Bitmap file</returns>
        private Bitmap DetermineImageFile()
        {
            Bitmap img;
            string filename;
            bool   foundimage = false;

            string[] files = this.listOfImages;

            // Filter list to remove non-images, last image shown,
            // and the app-generated BMP file from a previous run.
            // realy a sanity check because now everything gets filtered beforhand
            List <String> filteredFiles = new List <String> ();

            try
            {
                foreach (string file in files)
                {
                    string ext = file.Substring(file.LastIndexOf("."));

                    if (".jpg .bmp .gif .png .jpeg".IndexOf(ext.ToLower()) > -1 &&
                        !file.EndsWith("backroundcycler.bmp") &&
                        file != settings.LastImageShown &&
                        File.Exists(file))
                    {
                        filteredFiles.Add(file);
                    }
                }
            }
            catch
            {
                return(null);
            }

            // Make sure there are files left
            if (filteredFiles.Count == 0)
            {
                return(null);
            }
            do
            {
                // Randomly grab a file
                filename = filteredFiles[rnd.Next(filteredFiles.Count - 1)];

                img   = new Bitmap(filename);
                style = DetermineImageStyle(img);
                if (style == DesktopBackgroundStyle.Nothing)
                {
                    filteredFiles.Remove(filename);
                    if (filteredFiles.Count == 0)
                    {
                        return(null);
                    }
                    continue;
                }
                else if (style == DesktopBackgroundStyle.Scaled)
                {
                    Size imgSize = ScaledImageDimensions(img.Size, Screen.PrimaryScreen.Bounds.Size);
                    img   = new Bitmap(img, imgSize);
                    style = DesktopBackgroundStyle.Centered;
                }
                foundimage = true;
            } while (!foundimage);

            // Remember last image shown
            settings.LastImageShown = filename;

            // Return the bitmap object from this filename
            return(img);
        }