Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Changing the Desktop Wallpaper Programmatically

Author: DevASP
Download Source Code : 421_Set Wallpaper.zip

This article is about how you can set the background wallpaper of your computer using vb.net application.

  • Steps you will do.
  • Start visual studio 2005 and create a new window project. 
  • Set the following properties of the form

 

Text = "Set Wallpaper"

Size = “1024,750”

  • Now drip a picture box control on the form and set its following properties.

 

Size = “1024,725”

Sizemode = ”centerimage”

  • Drop a two button controls on the form and set its following properties as below.

 

First button control.

 

Name = " btgetimage"

Text = " Brows For Image"

 

Second button control.

 

Name = " btsetwallpaper"

Text = " Set Wallpaper"

 

  • Now drop an openfiledialog control on the form. 
  • Open you code window and import the following namespace.

 

Imports System.IO.Directory

 

  • Now declare the function and variables as below which will use win API's to set the wallpaper.

 

Private Const SPI_SETDESKWALLPAPER As Integer = &H14

Private Const SPIF_UPDATEINIFILE As Integer = &H1

Private Const SPIF_SENDWININICHANGE As Integer = &H2

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

Const WallpaperFile As String = "c:\wallpaper.bmp"

 

  • Make a function as below.

 

Friend Sub SetWallpaper(ByVal img As Image)

Dim imageLocation As String

imageLocation = My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.SpecialDirectories.MyPictures, WallpaperFile)

Try

img.Save(imageLocation, 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

  • Now in the click event of the first button write the following code to open and get the image.

OpenFileDialog1.InitialDirectory = "c:\"

OpenFileDialog1.Filter = "JPG|*.jpg|Bitmap|*.bmp"

Dim dialogresult As DialogResult = OpenFileDialog1.ShowDialog

If dialogresult = Windows.Forms.DialogResult.OK Then

PictureBox1.ImageLocation = OpenFileDialog1.FileName

btsetwallpaper.Enabled = True

End If

 

In the click event of the second button write following code to set the wallpaper.

 

SetWallpaper(Me.PictureBox1.Image)

MessageBox.Show("Wallpaper has been changed", "Set Wallpaper", MessageBoxButtons.OK, MessageBoxIcon.Information)

 

 
Article Comments
thank you - very clearly written.
i was wondering if you knew how to also change the desktop wallpaper style/position. For example, if it is Tile, change it to Stretch. Thanks!

Posted on 8/15/2006 4:42:06 PM by sougato

i wonder if theres a way to make it change desktop on load from the application?

Posted on 2/25/2007 8:29:17 PM by frost

this code is very useful..
thank you very much, many help for my college job

Posted on 4/20/2010 10:27:08 AM by Hasnul Arief

nice progrm

Posted on 11/16/2010 1:21:57 AM by raj verma

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get Your Computer Information Using VB.Net

Disclaimer - Privacy
© 2002-2017 DevASP.net