Change Desktop Wallpaper using a Jpeg file
I'm trying to write a simple program to change my desktop wallpaper. I'm using a downloaded jpeg file and I would like to convert it in code. The problem is the bitmap needs to be 24 bit to display. How do I do this? Thanks in advance.
public class ChangeWallpaper
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
public static void Main()
{
Bitmap wallbm = new Bitmap("pic.jpg");
wallbm.Save("pic.bmp");
SystemParametersInfo(20, 0, "pic.bmp", 0x01 |开发者_开发问答 0x02);
}
}
I couldn't get Clone to work for some reason. I was able to get it to work by trial and error by using the following code:
public class ChangeWallpaper
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
public static void Main()
{
Bitmap bm = new Bitmap(Image.FromFile("pic.jpg"));
bm.Save("pic.bmp", ImageFormat.Bmp);
SystemParametersInfo(20, 0, "pic.bmp", 0x01 | 0x02);
}
}
Use the Bitmap.Clone()
method and specify the desired pixel format.
精彩评论