0 Replies - 2002 Views - Last Post: 15 March 2016 - 03:23 PM

#1 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1536
  • View blog
  • Posts: 3,864
  • Joined: 01-February 13

Getting and Setting the Desktop Wallpaper

Posted 15 March 2016 - 03:23 PM

If by chance you have ever created some sort of an application for editing images or one that deals with images in some fashion, you may have thought about adding an option in your app to set an image as the desktop wallpaper. This is an option i have seen on several odd-n-end programs throughout the years so, i figured i would share this class to help you add this option.

You can set the image by passing a Bitmap or a String file path to the SetWallpaperImage function. You can also get the path or a Bitmap of the current wallpaper image using the GetWallpaperPath or GetWallpaperImage functions.

Add this WallpaperHelper class to your project.
Imports System.Runtime.InteropServices

Public Class WallpaperHelper
    Private Const SPI_SETDESKWALLPAPER As Integer = &H14
    Private Const SPI_GETDESKWALLPAPER As Integer = &H73
    Private Const SPIF_UPDATEINIFILE As Integer = &H1
    Private Const SPIF_SENDCHANGE As Integer = &H2

    <DllImport("user32.dll", EntryPoint:="SystemParametersInfoW")> _
    Private Shared Function SystemParametersInfoW(ByVal uiAction As UInteger, ByVal uiParam As UInteger, <MarshalAs(UnmanagedType.LPWStr)> ByVal pvParam As String, ByVal fWinIni As UInteger) As <MarshalAsAttribute(UnmanagedType.Bool)> Boolean
    End Function

    Public Shared Function GetWallpaperPath() As String
        Dim WallpaperPath As String = New String(Chr(0), 256)
        SystemParametersInfoW(SPI_GETDESKWALLPAPER, 255, WallpaperPath, 0)
        WallpaperPath = WallpaperPath.Remove(WallpaperPath.IndexOf(Chr(0)))
        Return WallpaperPath
    End Function

    Public Shared Function GetWallpaperImage() As Bitmap
        Dim wpBmp As Bitmap = Nothing
        Using tmp As New Bitmap(GetWallpaperPath()) 'create a temporary Bitmap of the wallpaper image
            wpBmp = New Bitmap(tmp) 'assign a new Bitmap of the temporary to the Bitmap to wpBmp. Doing this will break any ties we have to the file itself.
        End Using 'disposes the temporary bitmap
        Return wpBmp
    End Function

    Public Shared Function SetWallpaperImage(ByVal filename As String) As Boolean
    End Function

    Public Shared Function SetWallpaperImage(ByVal Bmp As Bitmap) As Boolean
        'The Walpaper image that the system uses is actually a copy of the image that you set it to in the desktop wallpaper settings. Different OS`s use different
        'places to store the copy of the image that the system uses. So, we need to have a known directory we can use to save a copy of the new wallpaper image too.
        'I recommend using a unique folder inside the ApplicationData folder.

        'This creates a Folder inside the ApplicationData folder with your application`s AssemblyName. This is where we save a copy of the image to use for the Wallpaper image.
        Dim MyAppDataFolder As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), My.Application.Info.AssemblyName)
        Dim WallPaperPath As String = IO.Path.Combine(MyAppDataFolder, "WallPaperImage.jpg") 'See comment below about saving as (.Jpg)

        If Not IO.Directory.Exists(MyAppDataFolder) Then IO.Directory.CreateDirectory(MyAppDataFolder) 'if the MyAppDataFolder folder does not exist, then create it

        Using b As New Bitmap(Bmp.Width, Bmp.Height, Imaging.PixelFormat.Format24bppRgb) 'we need to make sure the image is saved as a 24bpp RGB image
            Using g As Graphics = Graphics.FromImage(B)/>
                g.DrawImage(Bmp, 0, 0, Bmp.Width, Bmp.Height)
            End Using
            b.Save(WallPaperPath, Imaging.ImageFormat.Jpeg) 'save the image in our folder as a (.Jpg) image, same as Win7 creates and uses. (XP i believe uses .Bmp format)
        End Using

        'set the Wallpaper and return (True or False) depending on if the function succeded or not
    End Function
End Class

Here is a basic example of getting and setting the desktop wallpaper image.
Public Class Form1
    Private Img As Bitmap = Nothing

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        If Img IsNot Nothing Then Img.Dispose() 'dispose the class scoped Bitmap if it has been created
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Img = WallpaperHelper.GetWallpaperImage 'get the current Wallpaper image
        PictureBox1.Image = Img 'set the PictureBox to the current image
        Button_SetWallpaper.Enabled = False 'disable the SetWallpaper button
    End Sub

    Private Sub Button_SelectImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_SelectImage.Click
        Using ofd As New OpenFileDialog
            ofd.Filter = "Images|*.jpg;*.bmp;*.png"
            If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
                If Img IsNot Nothing Then Img.Dispose() 'if the Img has been set to a New Bitmap already, then dispose it before setting another New Bitmap to it
                Img = New Bitmap(ofd.FileName) 'assign a New Bitmap image to the Img variable
                PictureBox1.Image = Img 'set the PictureBox to the new bitmap of the image file that was selected
                Button_SetWallpaper.Enabled = True 'now we have selected a new image so, enable the SetWallpaper button
            End If
        End Using
    End Sub

    Private Sub Button_SetWallpaper_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_SetWallpaper.Click
        'the SetWallpaperImage function returns a Boolean variable that you can check to see if the wallpaper was set or not
        If Not WallpaperHelper.SetWallpaperImage(Img) Then
            MessageBox.Show("Failed to set wallpaper.")
        End If
        Button_SetWallpaper.Enabled = False 'now that you have set the Wallpaper to the currently selected Bitmap image, disable the SetWallpaper button
    End Sub
End Class

Is This A Good Question/Topic? 0
  • +

Page 1 of 1