[转载]C# 下载图片到本地 - zhanggang - 博客园

baacloud免费翻墙vpn注册使用

[转载]C# 下载图片到本地 – zhanggang – 博客园.

先建立一个类文件,然后建立函数,调用它就可以下载图片。

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
{
///
/// Download a page
///

/// The data downloaded from the page

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());
}
}

}
}
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏