Wednesday, January 27, 2010

Setting the wallpaper from C# code

Hi Friends,

What if you want to change the wallpaper of your desktop from C#.Net. for that you have to use Unmanaged code.

You have to import and pass parameters to function from User32 dll.
Here I am posting the code for performing the same.

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SystemParametersInfo(int Action, int Param, string lParam, int WinIni);
call the function and pass some useful parameters like ...
Bitmap bmp = new Bitmap(Image.FromFile(@"C:\mytest.jpg"));
bmp.Save("MyFile.Bmp", ImageFormat.Bmp);
SystemParametersInfo(20, 0, "pic.bmp", 0x1);

The first parameter 20 is for Wallpaper
Third parameter is your file path as String.
Last is Update parameter keep it 0X1, 0X01 | 0X02 ..

Note:- Here you see that I am converting the jpeg file into bmp file. Because it state that image should be 24 bits, so keep in mind the change of file else no effect of this function.

No comments:

Post a Comment