Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Integer, ByVal uParam As Integer, ByRef lpvParam As String, ByVal fuWinIni As Integer) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myPic As String = "YourPicture.jpg" 'This can be a gif, bmp, or jpg and probably others.
Dim image As Bitmap = New Bitmap(myPic)
'Might need to change the hard coded path to find the actual windows directory.
Dim path As String = "C:\Windows\" + System.IO.Path.GetFileNameWithoutExtension(myPic)
'This only works with bitmaps, so if it isn't a bmp file, it needs to be
'converted to one before we can change the wallpaper.
Dim ext As System.IO.FileInfo = New System.IO.FileInfo(myPic)
If ext.Extension.ToUpper <> "BMP" Then
image.Save(path, System.Drawing.Imaging.ImageFormat.Bmp)
End If
'Now make the call to change the wallpaper.
Dim returncode As Integer
returncode = SystemParametersInfo(20, 0, path, 0)
'Need to set the registry so that you can see the image when you right-click
'the desktop and choose properties and go to the background tab.
Dim key As Microsoft.Win32.RegistryKey
key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Control Panel", True)
Try
key = key.OpenSubKey("Desktop", True)
key.SetValue("Wallpaper", path)
Catch
'An error happened when setting the registry setting.
'Do what you want here.
End Try
'C# CODE FOR CHANGING THE WALLPAPER:
'AUTHOR Brian Russell AKA hellswraith on VBForums.com
'
'// This is the API declaration for the call to set the wallpaper.
'[System.Runtime.InteropServices.DllImportAttribute("user32")]
'private static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam,
' int fuWinIni);
'private void ImageToWallpaper()
'{
' // This only works with bitmaps, so we need to convert the image to
' // a bmp file before it will work. I am saving it in the C:\Windows folder
' string myPicPath = "YourPicture.jpg"
' Bitmap image = new Bitmap(myPicPath);
' string path = @"C:\Windows\" + System.IO.Path.GetFileNameWithoutExtension(myPicPath);
' image.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
' // Now make the call to change the wallpaper.
' int returncode;
' returncode = SystemParametersInfo(20,0,path,0);
' // We still need this. If we don't set the registry key
' // you won't see your image you just set in the properties window
' // when you right-click the desktop.
' RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel", true);
' Try
' {
' key = key.OpenSubKey("Desktop", true);
' key.SetValue("Wallpaper", path);
' }
' Catch
' {
' // Something went wrong.
' MessageBox.Show("Something went wrong");
' }
'}
End Sub