先建立一个类文件,然后建立函数,调用它就可以下载图片。
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace Spider
{
class DocumentWorker
{
/// <summary>
/// Download a page
/// </summary>
/// <returns>The data downloaded from the page</returns>
public void saveimage()
{
WebClient mywebclient = new WebClient();
string url = "http://images.google.cn/intl/zh-CN_ALL/images/images_hp.gif";
string newfilename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".jpg";
string filepath = @"F:\" + newfilename;
try
{
mywebclient.DownloadFile(url, filepath);
//filename = newfilename;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}