static void RevertWallpaper() { // EndDate is over so reset WindowsWallpaper.SetWallpaper(State.OriginalWallpaperPath); WindowsWallpaper.SetWallpaperStyle(State.OriginalWallpaperStyle); WindowsWallpaper.SetTileWallpaperValue(State.OriginalWallpaperTileValue); State.ImagesUsed.Clear(); State.DaysElapsed = 0; }
public void OverlaySmallerImage(ImageObject overlay) // Overlay a smaller image on top of this one (or if not smaller then resize) { Image overlayImageData = overlay.GetImage(); // Make sure overlay image is smaller than base image- resize if necessary int overlayWidth = overlayImageData.Width; int overlayHeight = overlayImageData.Height; int taskbarOffsetX = 0; int taskbarOffsetY = 0; string taskbarPos = WindowsWallpaper.GetTaskbarPosition(); if (!WindowsWallpaper.TaskbarIsHidden()) { if (taskbarPos == "Left" || taskbarPos == "Right") { // Taskbar uses space on x axis taskbarOffsetX = WindowsWallpaper.GetTaskbarHeight(); } else { // Taskbar uses space on y axis taskbarOffsetY = WindowsWallpaper.GetTaskbarHeight(); } } // If overlay is bigger in either dimension then shrink by 10% until it is smaller while (image.Width - taskbarOffsetX < overlayWidth || image.Height - taskbarOffsetY < overlayHeight) { overlayWidth = (int)(overlayWidth * 0.9); overlayHeight = (int)(overlayHeight * 0.9); } // Calculate coordinates such that overlay image is vertically at bottom of base image and horizontally centred int xValue = (image.Width / 2) - (overlayWidth / 2); int yValue = (image.Height - overlayHeight); if (taskbarPos == "Right" || taskbarPos == "Bottom") { // Need to subtract offset taskbarOffsetX *= -1; taskbarOffsetY *= -1; } xValue += taskbarOffsetX; yValue += taskbarOffsetY; using (Graphics g = Graphics.FromImage(image)) { // Must use rectange overload instead of point or overlay image may be resized g.DrawImage(overlayImageData, new Rectangle(xValue, yValue, overlayWidth, overlayHeight)); } }
static void UpdateWallpaper() { try { if (!(State.ImagesUsed.Count < State.Images.Count)) { // If there are not enough images and all have been used, then do nothing return; } // Choose a random image to overlay (or use base image for first day) string image; if (State.DaysElapsed == 0) { // The first day image = State.BaseImage; // Save location and style settings for current desktop image State.OriginalWallpaperPath = WindowsWallpaper.GetWallpaperPath(); State.OriginalWallpaperStyle = WindowsWallpaper.GetWallpaperStyle(); State.OriginalWallpaperTileValue = WindowsWallpaper.GetTileWallpaperValue(); // Save overlay string imagePath; State.Images.TryGetValue(image, out imagePath); ImageObject overlayImage = new ImageObject(imagePath); overlayImage.SaveImage(OverlayPath); State.ImagesUsed.Add(image); } else { image = RandomImage(); string imagePath; State.Images.TryGetValue(image, out imagePath); // Overlay image on top of existing overlay ImageObject overlayImage = new ImageObject(OverlayPath); ImageObject newImage = new ImageObject(imagePath); overlayImage.OverlayEqualImage(newImage); overlayImage.SaveImage(OverlayPath); State.ImagesUsed.Add(image); } // Overlay on desktop ImageObject wallpaper = WindowsWallpaper.GetWallpaper(); ImageObject overlay = new ImageObject(OverlayPath); wallpaper.OverlaySmallerImage(overlay); wallpaper.SaveImage(ModifiedWallpaperPath); WindowsWallpaper.SetWallpaper(Path.GetFullPath(ModifiedWallpaperPath)); State.DaysElapsed++; } catch (UnauthorizedAccessException) { MessageBox.Show("Unable to modify images, as program is unauthorised to access one of them", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); // Exit program with error code 5 (Access denied) Environment.Exit(5); } }