1. 程式人生 > >從URL獲取圖片並保存到本地

從URL獲取圖片並保存到本地

pos ons ring file timeout str oca pan load

     ///   <summary> 
        ///  HttpWebRequest Property
        ///   </summary> 
        ///   <param name="fileName">文件名稱</param> 
        ///   <param name="url">圖片地址</param> 
        ///   <param name="localPath">本地路徑("D:\\imgdow")</param>       
        public static
void DownloadOneFileByURL(string fileName, string url, string localPath) { Stream s = null; System.Net.HttpWebRequest request = null; System.Net.HttpWebResponse response = null; request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url); request.Timeout
= 5000; // 超時時間 response = (System.Net.HttpWebResponse)request.GetResponse(); s = response.GetResponseStream(); var imgs = Image.FromStream(s); imgs.Save(localPath + $"/{fileName}.jpg"); s.Close(); }

從URL獲取圖片並保存到本地