Wallpaper Changer

You can use the following code to change the desktop wallpaper programmatically. If you want the whole project, then you can download it from this link-

http://www.myotherdrive.com/dyn/pv/622.570005.14012009.91125.6a64fi/WallpaperChanger.rar?sort=0

public partial class Form1 : Form
{
private string path;
public Form1()
{
Hide();
InitializeComponent();

}

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 SystemParametersInfo
(
UInt32 action, UInt32 uParam, String vParam, UInt32 winIni
);

const int SPI_SETDESKWALLPAPER = 20;
const int SPIF_UPDATEINIFILE = 0×01;
const int SPIF_SENDWININICHANGE = 0×02;


/// <summary>
/// Form load event
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
private void Form1_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
this.Visible = false;
SetRandomImage();
timer1.Interval = 300 * 1000;
timer1.Start();
}

/// <summary>
/// Method to pick a image randomly from MyPictures
/// </summary>
private void SetRandomImage()
{
path = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
DirectoryInfo di = new DirectoryInfo(path);// + “\\” + “Auto Wallpaper”);
FileInfo[] rgFiles = di.GetFiles(“*.jpg”);
if (rgFiles.Length < 1)
{
MessageBox.Show(“No image files in this directory”);
Close();
}
Random rdImg = new Random();
int imgId = rdImg.Next(rgFiles.Length);
string strImgPath = ConvertImageToBmp(path + “\\” + rgFiles.GetValue(imgId));
SetWallpaper(strImgPath);
}

/// <summary>
/// Convert any other image extension to bmp and save it in temp foldere
/// </summary>
/// <param name=”strPath”>The path from which image is selected</param>
/// <returns>returns the new path of image in temp folder</returns>
private string ConvertImageToBmp(string strPath)
{
System.IO.Stream s = new System.Net.WebClient().OpenRead(strPath);
System.Drawing.Image img = System.Drawing.Image.FromStream(s);
string tempPath = Path.Combine(Path.GetTempPath(), “wallpaper.bmp”);
img.Save(tempPath, System.Drawing.Imaging.ImageFormat.Bmp);
return tempPath;
}

/// <summary>
/// Method to set the wallpaper
/// </summary>
/// <param name=”path”>The image location</param>
public void SetWallpaper(String path)
{
try
{
Form1.SystemParametersInfo(SPI_SETDESKWALLPAPER,
0,
path,
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}

catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}

/// <summary>
/// Timer Tick event
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
private void timer1_Tick(object sender, EventArgs e)
{
SetRandomImage();
}

/// <summary>
/// Exit the application on selection of close from the system tray icon menu
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
timer1.Stop();
Close();
}

/// <summary>
/// To display About The Product form from Tray bar icon
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
private void aToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show(“Wallpaper Changer. Copyright protected.”);
}

}

Leave a Reply