'CONSTANTS
Private Const SPI_SETDESKWALLPAPER As Integer = &H14
Private Const SPIF_UPDATEINIFILE As Integer = &H1
Private Const SPIF_SENDWININICHANGE As Integer = &H2
'API Declares
Private Declare Auto Function SystemParametersInfo Lib "user32.dll" ( _
ByVal uAction As Integer, ByVal uParam As Integer, _
ByVal lpvParam As String, ByVal fuWinIni As Integer) As Integer
Friend Sub SetWallpaper(ByVal img As Image, ByVal PathToImage As String)
Dim imageLocation As String
Dim FileName As String
imageLocation = PathToImage
Dim tmp = Split(imageLocation, "\")
FileName = tmp(UBound(tmp))
Try
img.Save(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) & FileName, System.Drawing.Imaging.ImageFormat.Bmp)
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, imageLocation, _
SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE)
Catch Ex As Exception
MsgBox("There was an error setting the wallpaper: " & Ex.Message)
End Try
End Sub