C# winform 更换电脑桌面壁纸

Wallpaper.cs
using System.Runtime.InteropServices;

namespace ABC
{
    public class Wallpaper
    {
        [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
        public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

        public void ChangeWallPaper(string bmpPath)
        {
            bmpPath = @"E:\wallpaper\Wallpaper1.bmp";

            SystemParametersInfo(20, 1, bmpPath, 0x1 | 0x2); //更换壁纸
        }
    }
}

调用

            if (Directory.Exists(@"E:\wallpaper")== false)
            {
                Directory.CreateDirectory(@"E:\wallpaper");
            }
            PictureBox pb = uiContextMenuStrip1.SourceControl as PictureBox;
            //保存图片为bmp格式
            pb.Image.Save(@"E:\wallpaper\Wallpaper1.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            string filePath = @"E:\wallpaper\Wallpaper1.bmp";
            Wallpaper wallpaper = new Wallpaper();  
            wallpaper.ChangeWallPaper(filePath);

图片必须是bmp格式的才能设为壁纸

posted @ 2022-01-19 17:31  帅气型男  阅读(461)  评论(0编辑  收藏  举报