代码
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Management;
using System.Net;
using System.Runtime.InteropServices;
using System.Windows.Forms;namespace SystemInfoWallpaper
{class WallpaperChanger{// 导入系统API用于设置壁纸[DllImport("user32.dll", CharSet = CharSet.Auto)]private static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);private const int SPI_SETDESKWALLPAPER = 20;private const int SPIF_UPDATEINIFILE = 0x01;private const int SPIF_SENDWININICHANGE = 0x02;static void Main(string[] args){try{// 获取原始图片路径(可以改为命令行参数或用户输入)string originalImagePath = GetOriginalImagePath();if (string.IsNullOrEmpty(originalImagePath) || !File.Exists(originalImagePath)){Console.WriteLine("找不到指定的图片文件!");return;}// 获取系统信息SystemInfo info = GetSystemInfo();Console.WriteLine("获取系统信息成功");// 处理图片,添加系统信息string outputImagePath = Path.Combine(Path.GetDirectoryName(originalImagePath), "wallpaper_with_info." + Path.GetExtension(originalImagePath).Trim('.'));if (AddInfoToImage(originalImagePath, outputImagePath, info)){Console.WriteLine("图片处理成功");// 设置为壁纸if (SetWallpaper(outputImagePath)){Console.WriteLine("壁纸设置成功!");}else{Console.WriteLine("壁纸设置失败!");}}else{Console.WriteLine("图片处理失败!");}}catch (Exception ex){Console.WriteLine($"发生错误: {ex.Message}");}Console.WriteLine("按任意键退出...");Console.ReadKey();}// 获取原始图片路径(这里使用用户选择的方式)private static string GetOriginalImagePath(){using (OpenFileDialog openFileDialog = new OpenFileDialog()){openFileDialog.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.bmp|所有文件|*.*";openFileDialog.Title = "选择要作为壁纸的图片";if (openFileDialog.ShowDialog() == DialogResult.OK){return openFileDialog.FileName;}}return null;}// 获取系统信息private static SystemInfo GetSystemInfo(){SystemInfo info = new SystemInfo();// 计算机名info.ComputerName = Environment.MachineName;// 操作系统信息info.OSVersion = GetOSVersion();// 开机时间info.Uptime = GetSystemUpTime();// IP地址info.IPAddress = GetIPAddress();return info;}// 获取操作系统版本private static string GetOSVersion(){ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem");foreach (ManagementObject os in searcher.Get()){return os["Caption"].ToString();}return Environment.OSVersion.VersionString;}// 获取系统运行时间private static string GetSystemUpTime(){ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT LastBootUpTime FROM Win32_OperatingSystem");foreach (ManagementObject os in searcher.Get()){DateTime lastBootUpTime = ManagementDateTimeConverter.ToDateTime(os["LastBootUpTime"].ToString());TimeSpan upTime = DateTime.Now - lastBootUpTime;return $"{upTime.Days}天 {upTime.Hours}小时 {upTime.Minutes}分钟";}return "未知";}// 获取IP地址private static string GetIPAddress(){string ipAddress = "未知";IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());foreach (IPAddress ip in host.AddressList){if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork){ipAddress = ip.ToString();break;}}return ipAddress;}// 在图片上添加系统信息private static bool AddInfoToImage(string inputPath, string outputPath, SystemInfo info){try{using (Image image = Image.FromFile(inputPath))using (Graphics graphics = Graphics.FromImage(image)){// 设置高质量绘图graphics.SmoothingMode = SmoothingMode.HighQuality;graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;// 定义字体和颜色Font font = new Font("微软雅黑", 12, FontStyle.Bold);Brush textBrush = new SolidBrush(Color.White);Brush backgroundBrush = new SolidBrush(Color.FromArgb(128, 0, 0, 0)); // 半透明黑色背景// 准备要显示的文本string[] lines = new string[]{$"计算机名: {info.ComputerName}",$"IP地址: {info.IPAddress}",$"操作系统: {info.OSVersion}",$"开机时间: {info.Uptime}"};// 计算文本所需的区域大小float maxWidth = 0;float totalHeight = 0;foreach (string line in lines){SizeF size = graphics.MeasureString(line, font);maxWidth = Math.Max(maxWidth, size.Width);totalHeight += size.Height;}// 文本区域的边距float margin = 10;maxWidth += margin * 2;totalHeight += margin * 2;// 计算右上角的位置float x = image.Width - maxWidth - margin;float y = margin;// 绘制半透明背景graphics.FillRectangle(backgroundBrush, x, y, maxWidth, totalHeight);// 绘制文本float currentY = y + margin;foreach (string line in lines){graphics.DrawString(line, font, textBrush, x + margin, currentY);currentY += graphics.MeasureString(line, font).Height;}// 保存处理后的图片ImageFormat format = GetImageFormat(inputPath);image.Save(outputPath, format);}return true;}catch (Exception ex){Console.WriteLine($"图片处理错误: {ex.Message}");return false;}}// 获取图片格式private static ImageFormat GetImageFormat(string filePath){string extension = Path.GetExtension(filePath).ToLower();switch (extension){case ".jpg":case ".jpeg":return ImageFormat.Jpeg;case ".png":return ImageFormat.Png;case ".bmp":return ImageFormat.Bmp;default:return ImageFormat.Jpeg; // 默认使用JPEG}}// 设置壁纸private static bool SetWallpaper(string imagePath){try{// 将图片复制到系统目录,因为有些系统设置壁纸时需要权限string systemPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "wallpaper_with_info.jpg");File.Copy(imagePath, systemPath, true);// 设置壁纸int result = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, systemPath, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);return result != 0;}catch (Exception ex){Console.WriteLine($"设置壁纸错误: {ex.Message}");return false;}}}// 系统信息类class SystemInfo{public string ComputerName { get; set; }public string IPAddress { get; set; }public string OSVersion { get; set; }public string Uptime { get; set; }}
}