c# - 更改桌面壁纸c#

标签 c# desktop wallpaper

我想创建一个程序,将 bmp 图像文件保存在驱动程序上并将该图像设置为壁纸。我设法编写的代码将图像保存在正确的位置,但图像没有显示为壁纸。请帮忙...

class Program
{
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern Int32 SystemParametersInfo(UInt32 uiAction, UInt32
    uiParam,String pvParam, UInt32 fWinIni);
    private static UInt32 SPI_SETDESKWALLPAPER = 20;
    private static UInt32 SPIF_UPDATEINIFILE = 0x1;
    private String imageFileName = "D:\\wall.bmp";


    static void Main(string[] args)
    {
        Bitmap bmp = new Bitmap(Properties.Resources.wall);
        bmp.Save("D:\\wall.bmp");
        SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, "D:\\wall.bmp", SPIF_UPDATEINIFILE);
    }





}

最佳答案

你可以尝试写这个类 Here :

public sealed class Wallpaper
{
    Wallpaper() { }

    const int SPI_SETDESKWALLPAPER = 20;
    const int SPIF_UPDATEINIFILE = 0x01;
    const int SPIF_SENDWININICHANGE = 0x02;

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

    public enum Style : int
    {
        Tiled,
        Centered,
        Stretched
    }

    public static void Set(Uri uri, Style style)
    {
        System.IO.Stream s = new System.Net.WebClient().OpenRead(uri.ToString());

        System.Drawing.Image img = System.Drawing.Image.FromStream(s);
        string tempPath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");
        img.Save(tempPath, System.Drawing.Imaging.ImageFormat.Bmp);

        RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
        if (style == Style.Stretched)
        {
            key.SetValue(@"WallpaperStyle", 2.ToString());
            key.SetValue(@"TileWallpaper", 0.ToString());
        }

        if (style == Style.Centered)
        {
            key.SetValue(@"WallpaperStyle", 1.ToString());
            key.SetValue(@"TileWallpaper", 0.ToString());
        }

        if (style == Style.Tiled)
        {
            key.SetValue(@"WallpaperStyle", 1.ToString());
            key.SetValue(@"TileWallpaper", 1.ToString());
        }

        SystemParametersInfo(SPI_SETDESKWALLPAPER,
            0,
            tempPath,
            SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
    }
}

祝你好运!

关于c# - 更改桌面壁纸c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16376394/

相关文章:

cocoa - 更改 OS X 10.7 Lion 中所有桌面上的壁纸?

c# - 如何在 OpenCVSharp 代码中使用新的 C++ 样式 API?

c# - 双方使用 stdcall 调用转换的 StackImbalance MDA 异常

c# - WPF:全屏同时显示两个窗口(扩展桌面)

c - 读取屏幕的一个像素(即您现在可以看到的 1024*768)

Android 应用程序使用定时器定期更换墙纸

C# - 将参数之间带有空格的参数传递给进程

c# - 在 C# 中从字符串创建动态类型

java - 动态壁纸强制接近场景

c++ - 使用 C++ 和 Windows API 以编程方式更改墙纸