C# 下载图片到本地
来源:互联网 发布:淘宝5金冠店铺多少钱 编辑:程序博客网 时间:2024/05/05 13:26
先建立一个类文件,然后建立函数,调用它就可以下载图片。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()); } } }}