Tuesday, March 20, 2007

Setting wallpaper programmatically

private const int SPI_SETDESKWALLPAPER = 0X14;
private const int SPIF_UPDATEINIFILE = 0X1;
private const int SPIF_SENDWININICHANGE = 0X2;


[DllImport("USER32.DLL",EntryPoint="SystemParametersInfo", SetLastError = true)]
private static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);


private const string WallpaperFile = "Saibaba.bmp";
//Place this picture in MyPictures folder.

internal void SetWallpaper(Image img)
{
string imageLocation;
imageLocation = System.IO.Path.GetFullPath (System.Environment.SpecialFolder.MyPictures.ToString() + WallpaperFile);

try
{
img.Save(imageLocation, System.Drawing.Imaging.ImageFormat.Bmp);
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, imageLocation, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}

catch (Exception Ex)
{
MessageBox.Show("There was an error setting the wallpaper: " + Ex.Message);
}
}

No comments: