Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  VS2005 - Setting Windows Desktop Wallpaper
 »  Home  »  Visual Studio 2005  »  VS2005 - Setting Windows Desktop Wallpaper
 »  Home  »  Windows Development  »  VS2005 - Setting Windows Desktop Wallpaper
VS2005 - Setting Windows Desktop Wallpaper
by Guest Author | Published  06/15/2004 | .NET Framework Visual Studio 2005 Windows Development | Rating:
Guest Author
This author account is for guest publications only, and does not reflect the bio for any particular author. 

View all articles by Guest Author...
VS2005 - Setting Windows Desktop Wallpaper

Guest Author: Jay Roxe

Here is some Visual Basic 2005 code for setting the Windows desktop wallpaper to an arbitrary image on the user's local drive. It illustrates use of VB2005 My classes, in this case to find the location of the user's My Pictures directory without a lot of coding.

To use, paste the following code into the top of your class module. This code requires Visual Basic 2005 to work.

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 ' change this to whatever filename you want to use Const WallpaperFile As String = "MovieCollectionImage.bmp" ''' <SUMMARY> ''' Sets the background of your Windows desktop. ''' The image will be saved in MyPictures and the background ''' wallpaper updated. ''' </SUMMARY> ''' The image to be set as the background. ''' <REMARKS></REMARKS> 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, use the following code to set the wallpaper to the desired image. This example shows how to set the wallpaper to an image contained in a Windows Forms picture box:

    SetWallpaper (Me.PictureBox1.Image)

And here's the code in C# as well (requires Visual C# 2005 to work):

private const int SPI_SETDESKWALLPAPER = 0X14; private const int SPIF_UPDATEINIFILE = 0X1; private const int SPIF_SENDWININICHANGE = 0X2; [DllImport("USER32.DLL",EntryPoint="SystemParametersInfo", SetLastError = true)] private static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni); // change this to whatever filename you want to use private const string WallpaperFile = "MovieCollectionImage.bmp"; // <SUMMARY> // Sets the background of your Windows desktop. // The image will be saved in MyPictures and the background // wallpaper updated. // </SUMMARY> // The image to be set as the background. // <REMARKS></REMARKS> internal void SetWallpaper(Image img) { string imageLocation; imageLocation = System.IO.Path.GetFullPath(System.Environment.SpecialFolder.MyPictures.ToString() + WallpaperFile); try { img.Save(imageLocation, System.Drawing.Imaging.ImageFormat.Bmp); SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, imageLocation, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE); } catch (Exception Ex) { MessageBox.Show("There was an error setting the wallpaper: " + Ex.Message); } }
How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:4.27027027027026 out of 5
 37 people have rated this page
Article Score32559
Comments    Submit Comment

Comment #1  (Posted by George Poth on 06/20/2004)

The more I read about the new Visual Basic version, the less I can wait for its release. It seems that some hobby programmers have not switched to .NET yet because it is as difficult to learn as C++ or Java. This is good so because there are still some people out there who say that Visual Basic is not a real programming language. With these and many other improvements in Visual Basic .NET, I find it very hard to believe that some people still find reasons not to switch.

It would have been a good idea if you had included some links to related articles. Your example will certainly make many people curious. Thanks for your efforts.
 
Comment #2  (Posted by Isaiah D. Williams on 07/02/2004)

There is a sneak peak of VB.Net 2005 on MSDN, you can find it here http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/vbnet2005_preview.asp. It tells you about most the features they have added.

HTH - Isaiah
 
Comment #3  (Posted by Issac on 07/06/2004)

I don't know about VB 2005...

Can you pls explain how to do the same with VB.NET...

Thanks in advance
 
Comment #4  (Posted by Paul on 08/29/2004)

If you goto microsofts website, burried deep within it is a beta for VB 2005 express that you can download. I don't remember the address, otherwise I would post it, but it took a long time to find on there.....some of the new features in it are nice!
 
Comment #5  (Posted by Yinon on 09/07/2004)

http://lab.msdn.microsoft.com/express/

 
Comment #6  (Posted by an unknown user on 01/23/2005)
Rating
It's cool coding but not effected in Bussines circle.
 
Comment #7  (Posted by an unknown user on 02/03/2005)
Rating
If you change this line
"imageLocation = My.Computer.FileSystem.CombinePath( _
My.Computer.FileSystem.SpecialDirectories.MyPictures, WallpaperFile)"

..into..

"imageLocation = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) & "\" & WallpaperFile"

It will work in VB.Net 2003
 
Comment #8  (Posted by an unknown user on 03/23/2005)
Rating
That is very good. I love it.
 
Comment #9  (Posted by Jay on 05/04/2005)
Rating
And Here's the code in C# as well:

private const int SPI_SETDESKWALLPAPER = 0X14;
private const int SPIF_UPDATEINIFILE = 0X1;
private const int SPIF_SENDWININICHANGE = 0X2;

[DllImport"USER32.DLL",EntryPoint="SystemParametersInfo", SetLastError = true)]
private static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);


// change this to whatever filename you want to use
private const string WallpaperFile = "MovieCollectionImage.bmp";

//
// Sets the background of your Windows desktop.
// The image will be saved in MyPictures and the background wallpaper
// updated.
//

// The image to be set as the background.
//
internal void SetWallpaper(Image img) {
string imageLocation;
imageLocation = System.IO.Path.GetFullPath(System.Environment.SpecialFolder.MyPictures.ToString() + WallpaperFile);
try {
img.Save(imageLocation, System.Drawing.Imaging.ImageFormat.Bmp);
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, imageLocation, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
} catch (Exception Ex) {
MessageBox.Show("There was an error setting the wallpaper: " + Ex.Message);
}
}
 
Comment #10  (Posted by an unknown user on 07/04/2005)
Rating
Hi this is good article if u can also tell how to set image as a screensaver then it would be great.
Kanika
 
Comment #11  (Posted by neha harwani on 07/23/2005)

hi
we have to do a project in vb by next week.. can u please tell me how to set the image in vb as wallpaper on desktop.we really need it urgently.
thanks in advance.please send me on my mail id.
 
Comment #12  (Posted by an unknown user on 08/21/2005)
Rating
Failed to function on my win server 2003 development pc - just blacked oiut the screen behind the form. Anything special about server 2003?
 
Comment #13  (Posted by Vahid on 10/25/2005)
Rating
cool, but how to do it in the VB.NET ?
please share if you have any algorithms about pasting tables from MSWord to VB6.0 or .NET and make it a Database.
 
Comment #14  (Posted by an unknown user on 02/02/2006)
Rating
i'm trying to paste your C# sample in C# .NET console project. i get the following error when i compile:

SetDesktop.cs(14): The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?)

any ideas?
thanks in advance
 
Comment #15  (Posted by Rob on 05/08/2006)
Rating
Your missing the includes. to get the Dllimport to not throw an error add this include

using System.Runtime.InteropServices;
 
Comment #16  (Posted by an unknown user on 05/10/2006)
Rating
You need to explain it better for people who are not geniouses. In mine half the the stuff wasn't declared.
 
Comment #17  (Posted by an unknown user on 08/03/2006)
Rating
This was excellent. It worked the very first time I tried it, and I am a relatively new VB.NET 2005 programmer. I was trying to do this before by setting registry values and a bunch of other stuff, all to no avail. Little did I know I needed the good ol' win32 api SystemParametersInfo
 
Comment #18  (Posted by an unknown user on 11/08/2006)
Rating
Just built this into a tiny VB 2005 app I'm putting together. It needs one more item...

If the user's computer has its Desktop image position set to Stretch or Center, how would I programmatically change it to Tile?


 
Comment #19  (Posted by an unknown user on 11/08/2006)
Rating
Just built this into a tiny VB 2005 app I'm putting together. It needs one more item...

If the user's computer has its Desktop image position set to Stretch or Center, how would I programmatically change it to Tile?


 
Comment #20  (Posted by an unknown user on 05/25/2007)
Rating
it workes
 
Comment #21  (Posted by an unknown user on 06/30/2007)
Rating
Hi, it is possible to set an hmtl desktop using this code?

 
Comment #22  (Posted by an unknown user on 08/14/2007)
Rating
this is very easily to view and impliment
 
Comment #23  (Posted by pravin on 10/19/2007)
Rating
hi, i tryied this code in c#.net 2005. i used the dllimport and all that is stated in this page. but when i try to call SystemParametersInfo function, the way it is given here, i am getting err that this function is unknown or not defined. pls help. thanks in advance !
 
Comment #24  (Posted by pravin parmar on 10/19/2007)
Rating
i got the soluction :). function declaration should be public. not private :)
 
Comment #25  (Posted by an unknown user on 02/03/2008)
Rating
To tile the image add this code prior:
Microsoft.Win32.Registry.SetValue_ ("HKEY_CURRENT_USER\Control_ Panel\Desktop", "TileWallpaper", 1,_ Microsoft.Win32.RegistryValueKind.String)
 
Comment #26  (Posted by an unknown user on 02/16/2008)
Rating
oo
 
Comment #27  (Posted by an unknown user on 02/16/2008)
Rating
oo
 
Sponsored Links