日期:2014-05-18  浏览次数:20505 次

远程采集
给定了网络图片的地址 如:
http://www.panjk.com/template/images/index/yuer.jpg
怎么把它保存到本地?
最好是asp.net + c#

------解决方案--------------------
public void ImageDownLoad(string TheURL, string filepath)
{
Uri temp = new Uri(TheURL);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(temp);
string page;
try
{
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Proxy = System.Net.WebProxy.GetDefaultProxy();//allow auto redirects from redirect headers
request.AllowAutoRedirect = true;//maximum of 10 auto redirects
request.MaximumAutomaticRedirections = 10;//30 second timeout for request
request.Timeout = (int)new TimeSpan(0, 0, 60).TotalMilliseconds;//give the crawler a name. //request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) ";//User-agent HTTP 标头的值。默认值为空引用
//request.UserAgent= "Mozilla/3.0 (compatible; My Browser/1.0) ";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
Bitmap map = new Bitmap(responseStream);

PictureBox picB = new PictureBox();

picB.Image = (Image)map;

string path = filepath.Substring(0, filepath.LastIndexOf( "\\ "));
picB.Image.Save(filepath);
}
/*catch(WebException ex)
{ //重新开始下载
return null;
}*/
catch (Exception ee)
{
page = "Fail message : " + ee.Message;
}
}
------解决方案--------------------

对 zhqs1000(子鱼) 的方法做了一下修改

public static bool DownLoadImage(string imageUrl, string filepath)
{
Uri _uri = new Uri(imageUrl);

if (_uri != null)
{
int _index = imageUrl.LastIndexOf( "/ ") + 1;

string _fileName = imageUrl.Substring(_index, imageUrl.Length - _index);

HttpWebRequest _request = (HttpWebRequest)WebRequest.Create(_uri);
try
{
_request.KeepAlive = false;
_request.ProtocolVersion = HttpVersion.Version10;
_request.Proxy = System.Net.WebProxy.GetDefaultProxy();//allow auto redirects from redirect headers
_request.AllowAutoRedirect = true;//maximum of 10 auto redirects
_request.MaximumAutomaticRedirections = 10;//30 second timeout for _request
_request.Timeout = (int)new TimeSpan(0, 0, 60).TotalMilliseconds;//give the crawler a name. //_request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) ";//User-agent HTTP 标头的值。默认值为空引用

HttpWebResponse _response = (HttpWebResponse)_request.GetResponse();
Stream _stream = _response.GetResponseStream();
Bitmap _bitmap = new Bitmap(_stream);

_bitmap.Save(filepath + "\\ " + _fileName);
}
catch
{
return false;
}
}
else
{