This question already has answers here:
Change desktop wallpaper using code in .NET
(4个答案)
5年前关闭。
我想创建一个程序,将bmp图像文件保存在驱动程序上,并将图像设置为墙纸。我设法编写的代码将图像保存在正确的位置,但是图像没有显示为墙纸。请帮忙...
祝好运!
(4个答案)
5年前关闭。
我想创建一个程序,将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/
10-10 13:33