如何解决用嵌入图像替换桌面壁纸
今天我在 .NET 中制作了一个控制台应用程序来替换桌面背景。
通过提供图像的路径,桌面背景将替换为该图像。
当前代码可以运行,但功能有限,就好像我让其他人运行编译后的 .exe 文件一样,如果他们的桌面上没有要替换的图像,则它不起作用。
因为可以将图像插入到应用程序中。我想知道是否可以将插入程序本身的图像设置为背景图像。
我看过几个关于 WPF 项目的教程。我需要一个在 .NET 控制台上运行的程序。
仅当图像已存在于预定义路径中时,我才会在当前代码下方工作:
class Program
{
public sealed class Wallpaper {
Wallpaper() { }
[DllImport("user32.dll",CharSet = CharSet.Auto)]
static extern int SystemParametersInfo(int uAction,int uParam,string lpvParam,int fuWinIni);
const int SPI_SETDESKWALLPAPER = 20;
const int SPIF_UPPDATEINIFILE = 0x01;
const int SPIF_SENDWININICHANGE = 0x02;
public enum Style : int //style enumeration
{
Fill,Fit
}
public static bool SetPath(System.Drawing.Image img,Style style)
{
try
{ //set temp folder path and image name
string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(),"Image1.bmp");
img.Save(tempPath,System.Drawing.Imaging.ImageFormat.Bmp);
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop",true);
//style values
if (style == Style.Fill)
{
key.SetValue(@"WallpaperStyle",10.ToString());
key.SetValue(@"TileWallpaper",0.ToString());
}
if (style == Style.Fit)
{
key.SetValue(@"WallpaperStyle",6.ToString());
key.SetValue(@"TileWallpaper",0.ToString());
}
//parameters info
SystemParametersInfo(SPI_SETDESKWALLPAPER,tempPath,SPIF_UPPDATEINIFILE | SPIF_SENDWININICHANGE);
return true;
}
catch
{
Console.WriteLine("Error");
return false;
}
}
public static void Setimage(string file_name,Style style)
{
try
{
System.Drawing.Image img = System.Drawing.Image.FromFile(System.IO.Path.GetFullPath(file_name));
SetPath(img,Style.Stretch);
img.dispose();
}
catch
{
Console.WriteLine("Error");
}
}
static public void Main(String[] args) {
//search the image in the path and set as background
string location = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string file_name = "windowsxp.jpg";
string fullPath = System.IO.Path.Combine(location,file_name);
Setimage(fullPath,Style.Stretch);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。