2016-03-21 26 views

回答

2

這個answer的問題How to download image from url using c#應該幫助你。

您可以使用Image.FromStream加載任何類型的常用位圖(jpg,png,bmp,gif,...),它會自動檢測文件類型,甚至不需要檢查url擴展名(它不是一個很好的做法)。例如:

using (WebClient webClient = new WebClient()) 
{ 
    byte [] data = webClient.DownloadData("https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-xpf1/v/t34.0-12/10555140_10201501435212873_1318258071_n.jpg?oh=97ebc03895b7acee9aebbde7d6b002bf&oe=53C9ABB0&__gda__=1405685729_110e04e71d9"); 

    using (MemoryStream mem = new MemoryStream(data)) 
    { 
     using (var yourImage = Image.FromStream(mem)) 
     { 
      // If you want it as Png 
      yourImage.Save("path_to_your_file.png", ImageFormat.Png) ; 

      // If you want it as Jpeg 
      yourImage.Save("path_to_your_file.jpg", ImageFormat.Jpeg) ; 
     } 
    } 

} 
0

您將需要使用像一個HttpClient的通話和流文件放回,然後可以用於將文件保存到一個HTTP響應或直接保存到磁盤上的文件流:

using (var httpClient = new HttpClient()) 
{ 
    using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri)) 
    { 
     using (
      Stream contentStream = await(await httpClient.SendAsync(request)).Content.ReadAsStreamAsync(), 
      stream = new FileStream("MyImage", FileMode.Create, FileAccess.Write, FileShare.None, Constants.LargeBufferSize, true)) 
     { 
      await contentStream.CopyToAsync(stream); 
     } 
    } 
} 

這當然是一個異步調用,因爲它可能是一個大文件。