To set the wallpaper in C# and VB.NET you can use the following snippet.

Sample C#

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

public const int SPI_SETDESKWALLPAPER = 20;
public const int SPIF_UPDATEINIFILE = 1;
public const int SPIF_SENDCHANGE = 2;

public static void SetWallpaper(string imagePath)
{
	if (File.Exists(imagePath))
	{
		SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, imagePath, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
	}
}

Sample VB.NET

<DllImport("user32.dll", CharSet := CharSet.Auto, SetLastError := True)> _
Public Shared Function SystemParametersInfo(uAction As Integer, uParam As Integer, lpvParam As String, fuWinIni As Integer) As Integer
End Function

Public Const SPI_SETDESKWALLPAPER As Integer = 20
Public Const SPIF_UPDATEINIFILE As Integer = 1
Public Const SPIF_SENDCHANGE As Integer = 2

Public Shared Sub SetWallpaper(imagePath As String)
	If File.Exists(imagePath) Then
		SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, imagePath, SPIF_UPDATEINIFILE Or SPIF_SENDCHANGE)
	End If
End Sub

One thought on “How to set the wallpaper in C# and VB.NET”

Leave a Reply