How to change Windows Desktop Wallpaper using C# and Windows APIs

By Mohammad Mahdi Ramezanpour at February 23, 2010 04:05
Filed Under: .NET General

It’s about a week that I’m working on an advertising application which manages customer’s advertising programs. One of the cool features that my customer requested was to change user’s desktop wallpaper into a specific photo or add some advertising notes to the current wallpaper.

So I started developing this application and faced something: “How to programmatically change Windows Desktop Wallpaper?

As you know, to change anything in Windows, you have to make use of Windows APIs. This one is the same. In order to change anything in Control Panel, There is an API function, named “SystemParametersInfo” :

The SystemParametersInfo function retrieves or sets the value of one of the system-wide parameters. This function can also update the user profile while setting a parameter.

How it works:

First of all you need to import System.Runtime.InteropServices namespace. This namespace enables you to import your favorite Windows API function library.

Note that SystemParametersInfo function is in User32.dll library.

To import a Windows API library in C#, it’s necessary to use “DLLImport” attribute:

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int SystemParametersInfo(int uAction,
int uParam,
string lpvParam,
int fuWinIni);
Now you have the function, so you have to use it somewhere:
 
string path = @"C:\Users\Public\Pictures\Sample Pictures\Desert.bmp";
const int SPI_SETDESKWALLPAPER = 20;
const int SPIF_UPDATEINIFILE = 0x01;
const int SPIF_SENDWININICHANGE = 0x02;

SystemParametersInfo(SPI_SETDESKWALLPAPER,
0,
path,
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);

Note that, the file you specify to be the wallpaper must be in Bitmap format. If you have JPEG, PNG, etc. files, you have to convert it to Bitmap, save it in a temp folder and then set it as Windows wallpaper.


Comments

2/23/2010 5:27:49 AM #

µ

I remember there was an API function for setting a .jpeg image as wallpaper in User32 library. Check MSDN for it.

µ Iran

2/23/2010 6:48:00 AM #

Admin

@Milad:
I don't know maybe there is an API. But this way is an standard way to do so. I believe setting a JPEG file a wallpaper costs more VGA process. Anyway, I'll check it out Smile

Admin United States

5/23/2010 2:04:23 AM #

Jaye Jahn

Hey - nice blog, just looking around some blogs, seems a pretty nice platform you are using. I'm currently using Wordpress

Jaye Jahn United States

5/24/2010 2:22:41 AM #

topills.com reviews

Opposite to other statements, greater researched articles still grab in subscribers like me. My first time here. I came across lots of intriguing stuff in the blog especially its discussion. So Many replies reveal I am not alone with this opinion

topills.com reviews United States

5/24/2010 6:45:00 AM #

make money with surveys

Fantastic post & Excellent blog! I would love to get started in a blog too but I have no clue where to start. I possess the ability to do it (not that hard on the practical part) but I really feel like I am too lazy to post frequently... That is the issue, if you start you need to go all the way...

make money with surveys United States

5/24/2010 7:19:14 AM #

free bingo

Hope to read more updates on your site soon!

free bingo United States

5/24/2010 9:12:51 PM #

designer handbags

I couldn't agree more

designer handbags United States

Comments are closed

Currently Reading

Quote of the day

Send Persian SMS