Ejemplo n.º 1
0
        public async Task RunScheduler()
        {
            // reset interval
            schedulerInterval = MainWindow._localSettings.WallpaperSource.interval;

            // set timer for next scheduler run
            MainWindow._bindings.LastUpdated = $"Last updated {DateTime.Now}";
            StartTimers(false);

            // download image, return its path, set as wallpaper and delete
            var imagePath = await _downloadHelper.DownloadAndSaveImage(MainWindow._localSettings.WallpaperSource.url.full);

            // process downloaded image
            if (!imagePath.Any())
            {
                MainWindow._bindings.ProgramStatus = "Download failed. Retrying shortly...";
                StartTimers(true);
            }
            else
            {
                _wallpaperHelper.SetWallpaper(imagePath);

                if (MainWindow._localSettings.KeepImages)
                {
                    _wallpaperHelper.MoveImage(imagePath);
                }
                else
                {
                    _wallpaperHelper.DeleteImage(imagePath);
                }
            }
        }
        private void DoWork(object state)
        {
            var count = Interlocked.Increment(ref _executionCount);

            _logger.LogInformation(
                "Timed Hosted Service is working. Count: {Count}", count);

            try
            {
                var unsplashApi  = new UnsplashApiClient(_config);
                var wallpaperCom = (IDesktopWallpaper)(new DesktopWallpaperClass());
                for (uint i = 0; i < wallpaperCom.GetMonitorDevicePathCount(); i++)
                {
                    var monitorId = wallpaperCom.GetMonitorDevicePathAt(i);

                    if (string.IsNullOrEmpty(monitorId))
                    {
                        continue;
                    }

                    var photo      = unsplashApi.GetRandomDesktopBackground().GetAwaiter().GetResult();
                    var outputPath = _config["outputPath"];
                    var imagePath  = string.IsNullOrEmpty(outputPath)
                        ? Path.Combine(Path.GetTempPath(), "wallpaper.jpg")
                        : Path.Combine(outputPath, $"{DateTime.Now:yyyyMMddHHmm}_{photo.Id}.jpg");
                    WallpaperHelper.SetWallpaper(wallpaperCom, monitorId, imagePath, new Uri(photo.Urls.Full));
                    unsplashApi.CallDownloadTrackingEndpoint(photo);
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
            }
        }
Ejemplo n.º 3
0
        public async static Task Main(string[] args)
        {
            var accessKey   = LoadAccessKey();
            var unsplashApi = new UnsplashApiClient(accessKey);
            var photo       = await unsplashApi.GetRandomDesktopBackground();

            WallpaperHelper.SetWallpaper(new Uri(photo.Urls.Full));
            unsplashApi.CallDownloadTrackingEndpoint(photo);
        }
Ejemplo n.º 4
0
        private void SetVirtualBackgroundImage()
        {
            //get the right virtual screen object, and draw a image for it
            var mon = _monitors.First(x => x.DeviceName == VirtualScreenName);

            using Image virtualScreenBitmap = new Bitmap(mon.Width, mon.Height);
            using var virtualScreenGraphic  = Graphics.FromImage(virtualScreenBitmap);
            mon.DrawToGraphics(virtualScreenGraphic);

            //save the image to hd and set it
            virtualScreenBitmap.Save(_wallpaperSetterSettings.VirtualWallpaperFilePath, ImageFormat.Jpeg);
            WallpaperHelper.SetWallpaper(_wallpaperSetterSettings.VirtualWallpaperFilePath, WindowsWallpaperStyle.Tile);
        }
Ejemplo n.º 5
0
        private void SetCombinedBackgroundImage()
        {
            //get the right true screen objects, and draw a image for it
            var mons = _monitors.Where(x =>
                                       x.DeviceName != VirtualScreenName &&
                                       x.DeviceName != LockScreenName);

            using Image virtualScreenBitmap = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
            using var virtualScreenGraphic  = Graphics.FromImage(virtualScreenBitmap);
            foreach (var mon in mons)
            {
                mon.DrawToGraphics(virtualScreenGraphic);
            }


            //save the image to hd and set it
            virtualScreenBitmap.Save(_wallpaperSetterSettings.CombinedWallpaperFilePath, ImageFormat.Jpeg);
            WallpaperHelper.SetWallpaper(_wallpaperSetterSettings.CombinedWallpaperFilePath, WindowsWallpaperStyle.Tile);
        }
Ejemplo n.º 6
0
        public void SetWallpaper()
        {
            if (Debugger.IsAttached)
            {
                //WallpaperHelper.SetWallpaper(WideImage, WindowsWallpaperStyle.Fit);
                //WallpaperHelper.SetWallpaper(WideImage, WindowsWallpaperStyle.Fill);
                //WallpaperHelper.SetWallpaper(WideImage, WindowsWallpaperStyle.Center);
                //WallpaperHelper.SetWallpaper(WideImage, WindowsWallpaperStyle.Span);
                //WallpaperHelper.SetWallpaper(WideImage, WindowsWallpaperStyle.Stretch);
                WallpaperHelper.SetWallpaper(WideImage, WindowsWallpaperStyle.Tile);

                //WallpaperHelper.SetWallpaper(HighImage, WindowsWallpaperStyle.Fit);
                //WallpaperHelper.SetWallpaper(HighImage, WindowsWallpaperStyle.Fill);
                //WallpaperHelper.SetWallpaper(HighImage, WindowsWallpaperStyle.Center);
                //WallpaperHelper.SetWallpaper(HighImage, WindowsWallpaperStyle.Span);
                //WallpaperHelper.SetWallpaper(HighImage, WindowsWallpaperStyle.Stretch);
                //WallpaperHelper.SetWallpaper(HighImage, WindowsWallpaperStyle.Tile);
            }
            else
            {
                Assert.Pass();
            }
        }
        /// <summary>
        /// Sets the current desktop wallpaper to the specified image.
        /// </summary>
        /// <param name="imagePath">The path of the image to set as the wallpaper.</param>
        public void SetWallpaper(string imagePath)
        {
            if (_settingWallpaper)
            {
                return;
            }
            if (!File.Exists(imagePath))
            {
                return;
            }

            _settingWallpaper = true;

            try
            {
                // get name cached
                var cachedImageName = GetCachedImageName(imagePath);

                // generate cached image if one doesn't exist
                if (!File.Exists(cachedImageName))
                {
                    var desktopSize = DesktopHelper.GetPrimaryDesktopSize();
                    var image       = Image.FromFile(imagePath);
                    ExifRotate(image);

                    Image wallpaper;

                    if (LandscapeOnly)
                    {
                        if (image.Width < image.Height)
                        {
                            _settingWallpaper = false;
                            ChangeWallpaper();
                            return;
                        }
                    }

                    var cropWallpaper = false;
                    if (desktopSize.Width > desktopSize.Height && image.Width > image.Height)
                    {
                        cropWallpaper = true;
                    }
                    else if (desktopSize.Width < desktopSize.Height && image.Width < image.Height)
                    {
                        cropWallpaper = true;
                    }

                    if (cropWallpaper)
                    {
                        wallpaper = ImageHelper.ScaleAndCropImageToFit(image, desktopSize);
                    }
                    else
                    {
                        var smallDesktopSize       = new Size(desktopSize.Width / 4, desktopSize.Height / 4);
                        var scaledBackgroundImage  = ImageHelper.ScaleAndCropImageToFit(image, smallDesktopSize);
                        var darkBackgroundImage    = ImageHelper.DarkenImage(scaledBackgroundImage, 0.5);
                        var blurredBackgroundImage = ImageHelper.BlurFilter(darkBackgroundImage, 10);

                        wallpaper = ImageHelper.ScaleAndCropImageToFit(blurredBackgroundImage, desktopSize);

                        var scaledImage = ImageHelper.ScaleImageToFit(image, desktopSize);

                        using (var graphics = Graphics.FromImage(wallpaper))
                        {
                            var x             = (desktopSize.Width - scaledImage.Width) / 2;
                            var y             = (desktopSize.Height - scaledImage.Height) / 2;
                            var imageLocation = new Point(x, y);
                            graphics.DrawImage(scaledImage, imageLocation);
                        }

                        scaledImage.Dispose();
                        scaledBackgroundImage.Dispose();
                        blurredBackgroundImage.Dispose();
                        darkBackgroundImage.Dispose();
                    }

                    if (ApplyMedianFilter)
                    {
                        using (var medianImage = ImageHelper.MedianFilter(wallpaper, 4))
                        {
                            medianImage.Save(cachedImageName, ImageFormat.Jpeg);
                        }
                    }
                    else
                    {
                        wallpaper.Save(cachedImageName, ImageFormat.Jpeg);
                    }


                    wallpaper.Dispose();

                    image.Dispose();
                }

                // get name for wallpaper bitmap in temp folder
                var wallpaperPath = Path.Combine(Path.GetTempPath(), "Wallpaper.bmp");

                // save cached image as wallpaper file and set as current wallpaper
                using (var image = Image.FromFile(cachedImageName))
                {
                    image.Save(wallpaperPath, ImageFormat.Bmp);
                }
                WallpaperHelper.SetWallpaper(wallpaperPath);

                // set as recent wallpaper
                CurrentWallpaperImage = imagePath;
                AddToRecentWallpaperList(imagePath);
            }
            catch (Exception e)
            {
                var message = $"Error setting wallpaper'{imagePath}'\r\n{e}";
                AddLogEntry(message);
                SaveLog();
            }
            finally
            {
                _settingWallpaper = false;
            }
        }
Ejemplo n.º 8
0
        //one for each screen --> direct to Windows API

        /// <summary>
        /// Sets given Wallpaper foreach Screen
        /// </summary>
        /// <param name="file">Wallpaper to set</param>
        public void SetSameWallOnEveryScreen(string file)
        {
            var attr = _wallpaperSetterSettings.WallpaperMode.GetAttribute <WallpaperModeWindowsMappingAttribute>();

            WallpaperHelper.SetWallpaper(file, attr.WindowsWallpaperStyle);
        }