2009-07-10 61 views
44

Làm cách nào để lưu hình ảnh từ URL? Tôi đang sử dụng C# và cần phải có thể lấy hình ảnh từ một URL và lưu trữ chúng tại địa phương. ... và không, tôi không ăn cắp :)Làm cách nào để lưu hình ảnh từ URL?

+0

Là hình ảnh một URL hoặc tham chiếu hình ảnh trong URL? – kenny

+0

Đây là URL cho một hình ảnh cụ thể –

Trả lời

100

Nó sẽ được dễ dàng hơn để viết một cái gì đó như thế này:

WebClient webClient = new WebClient(); 
webClient.DownloadFile(remoteFileUrl, localFileName);
+30

Heh ... nhưng không có nhiều dòng mã có nghĩa là bạn thông minh hơn? –

+0

Rất tuyệt. Các bạn nhanh quá! Cảm ơn. –

+4

Tuyệt vời, đừng quên 'Vứt bỏ()' của webclient – JDandChips

27

Bạn chỉ cần thực hiện yêu cầu http cơ bản bằng cách sử dụng HttpWebRequest cho URI của hình ảnh, sau đó lấy luồng byte kết quả rồi lưu luồng đó vào tệp.

Dưới đây là một ví dụ về cách để làm điều này ...

'Như một mặt lưu ý nếu hình ảnh là rất lớn, bạn có thể muốn chia tay br.ReadBytes (500000) vào một vòng lặp và lấy n byte tại một thời điểm viết mỗi lô byte khi bạn truy xuất chúng. '

using System; 
using System.IO; 
using System.Net; 
using System.Text; 

namespace ImageDownloader 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string imageUrl = @"http://www.somedomain.com/image.jpg"; 
      string saveLocation = @"C:\someImage.jpg"; 

      byte[] imageBytes; 
      HttpWebRequest imageRequest = (HttpWebRequest)WebRequest.Create(imageUrl); 
      WebResponse imageResponse = imageRequest.GetResponse(); 

      Stream responseStream = imageResponse.GetResponseStream(); 

      using (BinaryReader br = new BinaryReader(responseStream)) 
      { 
       imageBytes = br.ReadBytes(500000); 
       br.Close(); 
      } 
      responseStream.Close(); 
      imageResponse.Close(); 

      FileStream fs = new FileStream(saveLocation, FileMode.Create); 
      BinaryWriter bw = new BinaryWriter(fs); 
      try 
      { 
       bw.Write(imageBytes); 
      } 
      finally 
      { 
       fs.Close(); 
       bw.Close(); 
      } 
     } 
    } 
} 
+0

Bạn cũng có thể lặp cho đến cuối luồng thay vì br.ReadBytes (500000); – nos

+0

Điều này đặc biệt tốt cho tôi - tôi không cần tệp vật lý, chỉ là luồng, vì vậy nó tốt hơn nhiều so với câu trả lời được chấp nhận, trong trường hợp của tôi. – MGOwen

3

Một ví dụ trong aspx (C#)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Net; 
using System.IO; 

public partial class download_file_from_url : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     string url = "http://4rapiddev.com/wp-includes/images/logo.jpg"; 
     string file_name = Server.MapPath(".") + "\\logo.jpg"; 

     save_file_from_url(file_name, url); 

     Response.Write("The file has been saved at: " + file_name); 
    } 

    public void save_file_from_url(string file_name, string url) 
    { 
     byte[] content; 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
     WebResponse response = request.GetResponse(); 

     Stream stream = response.GetResponseStream(); 

     using (BinaryReader br = new BinaryReader(stream)) 
     { 
      content = br.ReadBytes(500000); 
      br.Close(); 
     } 
     response.Close(); 

     FileStream fs = new FileStream(file_name, FileMode.Create); 
     BinaryWriter bw = new BinaryWriter(fs); 
     try 
     { 
      bw.Write(content); 
     } 
     finally 
     { 
      fs.Close(); 
      bw.Close(); 
     } 
    } 
} 

Tác giả: HOÀN HUỲNH
ASP.Net C# Download Or Save Image File From URL

+0

Tôi đã sao chép từ [http://4rapiddev.com/csharp/asp-net-c-download-or-save-image-file-from-url/](http://4rapiddev.com/csharp/asp -net-c-download-hoặc-save-image-file-from-url /) – opsgreat

Các vấn đề liên quan