如何用代码将图片设为桌面背景
来源:互联网 发布:电脑太多网络怎么删除 编辑:程序博客网 时间:2024/05/21 00:55
将图片设为桌面背景可以通过修改注册表来实现,但是使用注册表后必须重新启动计算机才能生效,所以最好选择调用系统API来实现这个功能。
使用系统API的代码如下:
#region 调用API //引入user32.dll [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")] //声明SystemParametersInfo方法 static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, string lpvparam, Int32 fuwinIni); //声明常量SPI_SETDESKWALLPAPER private const int SPI_SETDESKWALLPAPER = 20; #endregion注意:在使用系统API时,由于API是非托管的,所以一定要引入
using System.Runtime.InteropServices;命名空间,然后将API引入当前环境中,这样才可以声明API中的方法。
引入“user32.dll”,声明SystemParametersInfo函数之后,获取图片的绝对路径,然后将路径作为参数传递到SystemParametersInfo函数中,就可以将图片设为桌面背景了。
string fullName = "image001.jpg"; //图片的全名string exName = fullName.Substring(fullName.LastIndexOf('.') + 1).ToLower(); //图片的扩展名string fullPath; //要设置为背景的原始图片的全路径fullPath = pPath + fullName; (注:pPath为图片所在文件夹的路径,如:E:\\图片 或 E:\\)if (pPath.Length != 4) //如果路径不是根目录,就要在中间加“\\”{ fullPath = pPath + "\\" + fullName;} if (exName == "bmp") //如果图片的类型是bmp,则调用API中的方法将其设置为桌面背景{ SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, fullPath, 1);}else //如果图片的类型不是bmp,则要先创建一个bmp格式的图像{ string fileName = fullName.Substring(0,fullName.LastIndexOf('.')); //获取图片名称,不带扩展名 string SystemPath = Environment.SystemDirectory; //获取系统路径 string newPath = SystemPath + "\\" + fileName + ".bmp"; //设置背景图片的系统文件路径 FileInfo fi = new FileInfo(newPath); //以这张图片的系统路径创建一个FileInfo的对象 if (fi.Exists) //检查newPath这个路径上是否有个指定为fileName的bmp类型的图片 { fi.Delete(); //如果有这样一个图片,就把它删除 PictureBox pb = new PictureBox(); pb.Image = Image.FromFile(fullPath); //以指定的原始图片的路径来创建一个新的PictureBox里面的Image对象 pb.Image.Save(newPath, ImageFormat.Bmp); //把这张新建的bmp类型的图片保存在系统文件路径里面 } else //如果指定的图片文件不存在 { PictureBox pb = new PictureBox(); pb.Image = Image.FromFile(fullPath); pb.Image.Save(newPath, ImageFormat.Bmp); } SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, newPath, 1); //调用SystemParametersInfo函数将指定的图片设为桌面背景}
- 如何用代码将图片设为桌面背景
- 图片无法设为桌面背景[日常问题笔记]
- stc单片机如何用C程序将IO口设为强推挽输出!!!
- 如何用MATLAB为图片加噪声
- GDI+ 将图片背景设为透明
- 将自己网站设为主页代码
- 如何用date将时间戳转换为时间格式
- 如何用date将时间戳转换为时间格式
- 如何用js代码将某个html标签重写
- 如何用myeclipse将wsdl文件生成java代码?
- 如何用git将项目代码上传到github
- 如何用git将项目代码上传到github
- 如何用git将项目代码上传到github
- 如何用git将项目代码上传到github
- 如何用git将项目代码上传到github
- 如何用git将项目代码上传到github
- 如何用git将项目代码上传到github
- 如何用git将项目代码上传到github
- 一骑绝尘引发的思考–关于hive程序员是否需要学习mapreduce
- Cannot find a free socket for the debugger
- UVA 11806 - Cheerleaders
- POJ2112--Optimal Milking
- [转载]NSSearchPathForDirectoriesInDomains用法
- 如何用代码将图片设为桌面背景
- 【基础知识】什么时候需要commit?
- 金龟子说的openeim
- vc++环境下的 udp 接收信息程序
- 关于USERCF和ITEMCF的那点事
- boost------bind的使用(Boost程序库完全开发指南)读书笔记
- C++的宏
- 世界顶级音响品牌排名
- php批量激活ucenter中的用户到Discuz x2.5论坛应用中