基本信息
源码名称:Bing壁纸浏览下载工具源码
源码大小:0.77M
文件格式:.rar
开发语言:C#
更新时间:2018-09-20
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
一个控制台程序(方便放到计划任务中定时下载),一个Winform程序(方便随时查看)。
很简单,纯粹闲的蛋疼搞了这么个东西,直接下载看吧。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BingImgForm
{
public partial class Form1 : Form
{
private const string baseUrl = "http://cn.bing.com";
private static ImageModel entity;
private static Image Img;
public Form1()
{
InitializeComponent();
label2.Text = "正在加载...";
label1.Parent = pictureBox1;
label2.Parent = pictureBox1;
comboBox1.Parent = pictureBox1;
}
private async void Form1_Load(object sender, EventArgs e)
{
string url = await GetBingImageAsync();
List<ImageList> list= entity.images;
comboBox1.DataSource = list;
comboBox1.ValueMember = "urlStr";
comboBox1.DisplayMember = "startdate";
pictureBox1.Image= UrlToImage(url.Split('|')[0]);
pictureBox1.Tag = url.Split('|')[1];
label2.Text = url.Split('|')[1];
comboBox1.SelectedIndexChanged = ComboBox1_SelectedIndexChanged;
label1.ForeColor = Color.White;
label2.ForeColor = Color.White;
}
private async void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label2.Text = "正在加载...";
string result = await ShowImageAsync(baseUrl comboBox1.SelectedValue.ToString().Split('|')[0]);
pictureBox1.Image = Img;
pictureBox1.Tag = comboBox1.SelectedValue.ToString().Split('|')[1];
label2.Text = comboBox1.SelectedValue.ToString().Split('|')[1];
}
/// <summary>
/// 异步获取Image实体
/// </summary>
/// <returns></returns>
static Task<string> GetBingImageAsync()
{
return Task<string>.Run(() =>
{
string url = "http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=100";
string _ret = HttpHelper.SendPostApi(url, null);
//entity = JsonHelper.ConvertToObject<ImageModel>(_ret);
entity = JsonHelp.ParseFormJson<ImageModel>(_ret);
return baseUrl entity.images[0].url "|" entity.images[0].ImageTitie;
});
}
/// <summary>
/// 异步转换图片Url到Image对象
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
static Task<string> ShowImageAsync(string url)
{
return Task<string>.Run(() =>
{
Img= UrlToImage(url);
return "ok";
});
}
#region 将图片Url转换为Image对象
private static Image UrlToImage(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using (WebResponse response = request.GetResponse())
{
Image img = Image.FromStream(response.GetResponseStream());
return img;
}
}
#endregion
#region 下载
private void 下载ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = ".jpg";
sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
sfd.Filter = "图片文件|*.jpg";
sfd.FileName = pictureBox1.Tag ".jpg";
if (sfd.ShowDialog() == DialogResult.OK)
{
bool b = HttpHelper.DownloadFile(baseUrl comboBox1.SelectedValue.ToString().Split('|')[0], sfd.FileName);
if (b)
label2.Text = "下载完成!";
else
label2.Text = "下载失败!";
}
}
private void 保存到D盘根目录ToolStripMenuItem_Click(object sender, EventArgs e)
{
bool IsDrive= Directory.Exists(@"D:\");
if (IsDrive)
{
bool b = HttpHelper.DownloadFile(baseUrl comboBox1.SelectedValue.ToString().Split('|')[0], "d:\\" pictureBox1.Tag ".jpg");
if (b)
label2.Text = "下载完成,已成功保存到D:\\。";
else
label2.Text = "下载失败。";
}
else
MessageBox.Show("无法保存,因为D盘不存在!","提示",MessageBoxButtons.OK,MessageBoxIcon.Stop);
}
#endregion
}
}