下载远程图片

1.

 public Bitmap Get_img()
        {
            Bitmap img = null;
            HttpWebRequest req;
            HttpWebResponse res = null;
            try
            {
                
                System.Uri httpUrl = new System.Uri("http://xxx.com/xxx.png ");
                req = (HttpWebRequest)(WebRequest.Create(httpUrl));
                req.Timeout = 180000; //设置超时值10秒
                //req.UserAgent = "XXXXX";
                //req.Accept = "XXXXXX";
                req.Method = "GET";
                res = (HttpWebResponse)(req.GetResponse());
                img = new Bitmap(res.GetResponseStream());//获取图片流                
                img.Save(@"E:/" + DateTime.Now.ToFileTime().ToString() + ".png");//随机名
            }

            catch (Exception ex)
            {
                string aa = ex.Message;
            }
            finally
            {
                res.Close();
            }
            return img;
        }

2.较简单

public void DownImage(string Url, string Path)
{
System.Net.WebClient web = new System.Net.WebClient();
web.DownloadFile(Url, Path);

}

posted @ 2015-12-16 09:16  立于群  阅读(66)  评论(0)    收藏  举报