用嵌入图像替换桌面壁纸

问题描述

今天我在 .NET 中制作了一个控制台应用程序来替换桌面背景。

通过提供图像的路径,桌面背景将替换为该图像。

当前代码可以运行,但功能有限,就好像我让其他人运行编译后的 .exe 文件一样,如果他们的桌面上没有要替换的图像,则它不起作用。

因为可以将图像插入到应用程序中。我想知道是否可以将插入程序本身的图像设置为背景图像。

enter image description here

如何修改当前代码,将程序内部的图片设置为桌面背景?

我看过几个关于 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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...