【问题标题】:fit / fill image?适合/填充图像?
【发布时间】:2014-08-21 19:43:39
【问题描述】:

我正在制作一个简单的程序来改变我的电脑背景。我在网上找到了一个 stackoverflow 问题,或多或少涵盖了我想做的事情。我现在可以成功地将我的壁纸更改为平铺、居中和从在线图像 URL 拉伸。但是,在控制面板中,可以选择将壁纸置于“适合”和“填充”位置。如何以编程方式将壁纸设置为适合/填充模式?

相关代码:

public enum Style : int
{
    Tiled,
    Centered,
    Stretched
}
public class Wallpaper
{
    public 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 void Set(string URL, Style style)
    {
        HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
        HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
        Stream stream = httpWebReponse.GetResponseStream();
        System.Drawing.Image img = Image.FromStream(stream);

        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# .net wallpaper


    【解决方案1】:

    这可能会对你有所帮助。

    摘自Set the desktop wallpaper

       case WallpaperStyle.Fit: // (Windows 7 and later)
           key.SetValue(@"WallpaperStyle", "6");
           key.SetValue(@"TileWallpaper", "0");
           break;
       case WallpaperStyle.Fill: // (Windows 7 and later)
           key.SetValue(@"WallpaperStyle", "10");
           key.SetValue(@"TileWallpaper", "0");
           break;
    

    我相信您可以轻松地将其适应您的代码。

    【讨论】:

    • 这正是我所需要的!谢谢你。我找不到文档。
    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多