HOWTO · Csharp

在 C# 中下載圖片

有 3 種主要方法可用於下載 C# 中的圖片,WebClient.DownloadFile()函式,Bitmap 類和 Image.FromStream()函式。

本教程將討論使用 C# 下載圖片的方法。

在 C# 中使用 WebClient 類下載圖片

WebClient提供了用於向 C# 中的 URL 傳送資料和從 URL 接收資料的功能。WebClient.DownloadFile(url, path) 函式從特定的 URL url 下載檔案,並將其儲存到 path。我們可以使用 WebClient.DownloadFile() 函式從 URL 下載圖片。

using System;
using System.Net;
using System.Runtime.InteropServices;

namespace download_image {
  class Program {
    static void download() {
      string url =
          "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Breathe-face-smile.svg/1200px-Breathe-face-smile.svg.png";
      using (WebClient client = new WebClient()) {
        client.DownloadFile(new Uri(url), "Image.png");
      }
    }
    static void Main(string[] args) {
      try {
        download();
      } catch (ExternalException e) {
        Console.WriteLine(e.Message);

      } catch (ArgumentNullException e) {
        Console.WriteLine(e.Message);
      }
    }
  }
}

在上面的程式碼中,我們使用 C# 中的 client.DownloadFile(new Uri(url), "Image.png") 函式從 URL url 下載圖片並將其儲存到路徑 Image.png

使用 C# 中的 Bitmap 類在不知道格式的情況下下載圖片

在上面的示例中,我們必須知道要下載的圖片檔案格式;然後,我們可以從 URL 下載並儲存。但是,如果我們不知道圖片檔案格式,則可以使用 Bitmap 類。Bitmap提供了使用 C# 處理圖片的方法。該方法將以 Bitmap 類可以處理的所有格式下載和儲存檔案。Bitmap.Save(path, format) 函式將我們點陣圖的內容寫入格式為 formatpath 中的檔案。ImageFormat可以在 Bitmap.Save() 函式內部使用,以手動指定要儲存的圖片格式。以下程式碼示例向我們展示瞭如何在不使用 C# 中的 Bitmap.Save() 函式知道格式的情況下從 URL 下載圖片。

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;

namespace download_image {
  class Program {
    static void SaveImage() {
      string url =
          "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Breathe-face-smile.svg/1200px-Breathe-face-smile.svg.png";
      WebClient client = new WebClient();
      Stream stream = client.OpenRead(url);
      Bitmap bitmap = new Bitmap(stream);

      if (bitmap != null) {
        bitmap.Save("Image1.png", ImageFormat.Png);
      }

      stream.Flush();
      stream.Close();
      client.Dispose();
    }
    static void Main(string[] args) {
      try {
        SaveImage();
      } catch (ExternalException e) {
        Console.WriteLine(e.Message);

      } catch (ArgumentNullException e) {
        Console.WriteLine(e.Message);
      }
    }
  }
}

我們定義了 SaveImage() 函式,該函式可以下載並儲存圖片。我們使用 WebClient 類向 url 發出 Web 請求。我們使用了 Stream 類從 client.OpenRead(url) 函式中讀取資料。我們使用了 Bitmap 類將流轉換為點陣圖格式。然後,我們使用 bitmap.Save() 函式將 bitmap 儲存在路徑 Image1.png 中,格式為 ImageFormat.Png

使用 C# 中的 Image.FromStream() 函式下載不知道格式的圖片

我們還可以使用 C# 中的 Image.FromStream() 函式來完成與上一個示例相同的操作。Image.FromStream() 函式從 C# 的記憶體流中讀取圖片檔案。為此,我們可以先將 URL 中的所有資料下載到位元組陣列中。然後,我們可以將該陣列載入到 MemoryStream的物件中。然後,我們可以使用 Image.FromStream() 函式從 MemoryStream 類的物件讀取圖片。然後,我們可以使用 C# 中的 Image.Save(path, format) 函式將該圖片儲存為特定格式的路徑。以下程式碼示例向我們展示瞭如何在不使用 C# 中的 Image.FromStream() 函式知道其格式的情況下,從 URL 下載圖片。

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;

namespace download_image {
  class Program {
    static void SaveImage() {
      string url =
          "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Breathe-face-smile.svg/1200px-Breathe-face-smile.svg.png";
      using (WebClient webClient = new WebClient()) {
        byte[] data = webClient.DownloadData(url);

        using (MemoryStream mem = new MemoryStream(data)) {
          using (var yourImage = Image.FromStream(mem)) {
            yourImage.Save("Image2.png", ImageFormat.Png);
          }
        }
      }
    }
    static void Main(string[] args) {
      try {
        SaveImage();
      } catch (ExternalException e) {
        Console.WriteLine(e.Message);

      } catch (ArgumentNullException e) {
        Console.WriteLine(e.Message);
      }
    }
  }
}

我們定義了 SaveImage() 函式,該函式可以下載並儲存圖片。我們使用了位元組陣列資料來儲存 webClient.DownloadData(url) 函式返回的資料。然後,我們用 data 初始化了 MemoryStream 類的例項 mem。然後,使用 Image.FromStream(mem) 函式從 mem 中讀取圖片 yourImage。最後,通過使用 yourImage.Save("Image2.png", ImageFormat.Png) 函式,以 ImageFormat.Png 格式將圖片 yourImage 儲存到 Image2.png 路徑。