Refer to this article:
Http://blog.csdn.net/sonyicn/archive/2006/05/20/746280.aspx
The actual code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Runtime. InteropServices;
Using Microsoft. Win32;
Using System. IO;
Namespace RockLib
{
Public class Utils
{
/** // <Summary>
/// Change user's desktop wallpaper
/// </Summary>
/// <Param name = "picturePath"> </param>
/// <Param name = "style"> </param>
/// <Returns> </returns>
Public static bool ChangeWallPaper (string picturePath, WallPaperStyle style)
{
RegistryKey myRegKey = Registry. CurrentUser. OpenSubKey ("Control Panel \ desktop", true );
Switch (style)
{
Case WallPaperStyle. Center:
MyRegKey. SetValue ("TileWallpaper", "0 ");
MyRegKey. SetValue ("WallpaperStyle", "0 ");
Break;
Case WallPaperStyle. Tile:
MyRegKey. SetValue ("TileWallpaper", "1 ");
MyRegKey. SetValue ("WallpaperStyle", "0 ");
Break;
Case WallPaperStyle. Stretch:
MyRegKey. SetValue ("TileWallpaper", "0 ");
MyRegKey. SetValue ("WallpaperStyle", "2 ");
Break;
Default:
Throw new NotSupportedException ("style ");
}
MyRegKey. Close ();
Int intResult;
If (! File. Exists (picturePath ))
{
Throw new ArgumentException (string. Format ("Picture {0} cannot be found.", picturePath ));
}
IntResult = SystemParametersInfo (20, 3, picturePath, 0x1 | 0x2 );
Return intResult> 0;
}
[DllImport ("user32.dll", CharSet = CharSet. Auto)]
Public static extern int SystemParametersInfo
(Int uAction, int uParam, string lpvParam, int fuWinIni );
}
Public enum WallPaperStyle
{
Center,
Tile,
Stretch
}
}
Test code:
Code
Private void button2_Click (object sender, EventArgs e)
{
String filePath = AppDomain. CurrentDomain. BaseDirectory + "\ untitled.bmp ";
If (! Utils. ChangeWallPaper (filePath, WallPaperStyle. Tile ))
{
MessageBox. Show ("Change failed .");
}
}