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格式的才能设为壁纸

相关文章:

  • 2021-09-30
  • 2021-12-03
  • 2021-11-19
  • 2020-04-08
  • 2022-01-02
  • 2021-12-12
猜你喜欢
  • 2022-01-02
  • 2021-12-15
  • 2021-09-30
  • 2021-12-29
  • 2021-12-15
  • 2021-08-06
相关资源
相似解决方案