Changing the Wallpaper (Source Code)
Wed, 13 May 2009 17:59:55 -0400 - Author:
This small program regularly changes the desktop wallpaper using images in a folder. Add the System.Drawing reference to a new project, build the project, put the EXE file in a directory with many wallpapers and run it.
/* * (C) 2008 Peter O. * Permission is granted to anyone to copy, modify, * and redistribute this file. Attribution to the * author would be appreciated but is not required. * No warranties are provided and all liabilities * are disclaimed. */ using System; using System.IO; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; using System.Threading; using System.Diagnostics;
namespace WallpaperRotator { class Program { [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool SystemParametersInfo( int uiAction, uint uiParam, string pvParam, int fWinIni);
public static Random random=new Random(); public static void NextWallpaper(){ List<string> files=new List<string>(); // Get a list of all wallpapers in the directory files.AddRange(Directory.GetFiles(".","*.jpg")); files.AddRange(Directory.GetFiles(".","*.jpeg")); // Leave if no wallpapers exist if(files.Count==0) return; // Choose a random wallpaper in the directory string file=files[random.Next(files.Count)]; // Save to bitmap for SystemParametersInfo using(Bitmap b=(Bitmap)Bitmap.FromFile(file)){ b.Save("wallpaper.bmp",System.Drawing.Imaging.ImageFormat.Bmp); } // Set the bitmap as wallpaper SystemParametersInfo(20,1, Path.GetFullPath("wallpaper.bmp"),3); } public static void Main(string[] args) { while(true){ // Change the wallpaper NextWallpaper(); // Wait 60 seconds between displaying wallpapers Thread.Sleep(60*1000); } } } }
