/// <summary>
        ///
        /// </summary>
        /// <param name="image">XML解析的Image对象</param>
        /// <param name="saveImagesFolderLocation">本地保存地址</param>
        public static string saveImage(BingImage image, string saveImagesFolderLocation, Boolean useWaterImage)
        {
            string location = "";
            //设置墙纸
            Bitmap     bmpWallpaper;
            WebRequest webreq = WebRequest.Create(Config.UrlPer + image.Url);
            //Console.WriteLine(getURL());
            //Console.ReadLine();
            WebResponse webres = webreq.GetResponse();

            using (Stream stream = webres.GetResponseStream())
            {
                bmpWallpaper = (Bitmap)Image.FromStream(stream);
                //stream.Close();
                if (!Directory.Exists(saveImagesFolderLocation))
                {
                    Directory.CreateDirectory(saveImagesFolderLocation);
                }
                //设置文件名为例:bing_2017816_title.jpg
                location = saveImagesFolderLocation + "\\bing_" + image.StartDate + "_" + image.Title + ".jpg";
                bmpWallpaper.Save(location, ImageFormat.Jpeg);
            }
            if (useWaterImage)
            {
                MyWaterImageService.Process(location, ImagePosition.RigthBottom, saveImagesFolderLocation);
            }
            return(location);
        }
Ejemplo n.º 2
0
        private void GetImage()
        {
            tip.Text = "";
            if (String.IsNullOrEmpty(tbFileDirectory.Text))
            {
                tip.Text      = "请选择文件路径";
                tip.ForeColor = Color.OrangeRed;
                return;
            }
            BingImage image = null;

            try
            {
                image = BingWallpaperService.getURL(cbInternational.Checked?Config.SiteType.International : Config.SiteType.znCN, 1)[0];
            }
            catch (Exception ex)
            {
                tip.Text      = "获取地址失败:" + ex.Message;
                tip.ForeColor = Color.Red;
                return;
            }

            try
            {
                string location = BingWallpaperService.saveImage(image, tbFileDirectory.Text, cbUseWatermark.Checked);
                if (checkBox1.Checked)
                {
                    setWallpaperApi(location);
                    tip.Text = "壁纸设置成功";
                }
                else
                {
                    tip.Text = "保存成功";
                }
                tip.ForeColor = Color.Black;
            }
            catch (Exception ex)
            {
                tip.Text      = "发生错误:" + ex.Message;
                tip.ForeColor = Color.Red;
                return;
            }
        }