小创意之-C#设置电脑壁纸

小创意— 设置电脑壁纸

看着面黄肌肉的电脑壁纸,默默打卡了百度,搜索美丽的壁纸。看着风格古怪,分辨率里五五六六的图片。默默的流了几滴汗。于是单生了一个念头,为什么不抓去合适的图片自动设置成电脑壁纸呢?于是便用C#写了一个小程序。

寻找合适的代码图片下载到本地

提供几个常见的图片网址

https://unsplash.it/1600/900?random(国外的随机图片)

https://uploadbeta.com/api/pictures/random/?key=BingEverydayWallpaperPicture(必应每日图片)

https://uploadbeta.com/api/pictures/random(随机图片)

通过C#下载图片和预览图片


             // 图片下载下来 保存到本地
            string url = "https://uploadbeta.com/api/pictures/random/";
             System.Net.WebClient mywebclient = new System.Net.WebClient();
             mywebclient.DownloadFile(url, filePath);
            // this.pictureBox1.Load(filePath);
            // 把图片预览在C#的 pictureBox控件
            FileStream pFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            pictureBox1.Image = Image.FromStream(pFileStream);
            pFileStream.Close();
            pFileStream.Dispose();

调用win的库 设置为壁纸

 /// <summary>
        ///  图片设置为背景图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {       
            MessageBox.Show(filePath);
            SystemParametersInfo(20,1,filePath,1);
        }

        [DllImport("user32.dll",EntryPoint= "SystemParametersInfo")]
        public static extern int SystemParametersInfo(
            int uAction,
            int uParam,
            string lpvParam,
            int fuWinIni
            );
    }

最后样式

窗体设置样式

image-20200514225248365

运行效果

image-20200514225305663 image-20200514225440199

具体的代码在github连接里面

posted @ 2020-05-14 23:20  妖君你好  阅读(382)  评论(0编辑  收藏  举报