Setting Desktop Background
I recently bought a subscription to www.digitalblasphemy.com because I like the guys wallpapers and hes got a lot of good dual monitor wallpapers. After I downloaded a bunch of them, I was having trouble settling on one to use for my desktop, so I wanted to write a little app that would pick one at random when I logged on and set it as my desktop background.
After googling for how to set your desktop background in C#, I saw tons of examples that used the Win32 api call SystemParametersInfo. So I tried using that function in the same way that the examples were using it, but it just wouldn’t take, all I would get was a dark blue background and the function also returned 0, which indicated that an error had occurred.
After some more digging, I found out that SystemParametersInfo only works if you are supplying a .bmp file, whereas my files were .jpg’s. So, I did some more digging and found that theres a different function to use for files other than bitmaps, the aptly named SetWallpaper function on the IActiveDesktop interface.
I could only find one example of someone who used this interface in c# to set the desktop background, but I couldn’t get it to work, I kept getting an error about wrong GIUD’s for the COM interfaces. What I ended up doing is using the old SysteParametersInfo function, and after selecting a random jpeg, I would save it out as a bitmap (if the bitmap for that file didn’t already exist) and then pass that bitmap into the function. Ugh, ugly but it gets the job done. I could have simply found the registry key that controls the desktop background and set it that way, and maybe I will if I get too annoyed at these extra bitmaps laying around, but I wanted to do it using the appropriate api.
The point of all this was simply to inform you that SystemParametersInfo only takes bitmaps, not jpegs. Have a happy Thanksgiving, all you Americans.
Anonymous said,
Wrote on November 23, 2005 @ 12:53 pm
see my solution here:
http://ebersys.blogspot.com/2005/11/set-wallpaper-in-c-using.html
Soup_dog said,
Wrote on August 28, 2006 @ 8:49 am
Too funny. I could have written your comment myself. I too signed up with digitalblasphemy, and I too wanted to get the two monitor backgrounds working. I am currently searching the web for a java example of how to change your desktop image. If I ever find a solution I will let you know. Good luck!