日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

在 C# 中下载图片

發(fā)布時間:2023/12/15 C# 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在 C# 中下载图片 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
  • 使用 C# 中 WebClient 類下載圖片
  • 使用 C# 中的 Bitmap 類在不知道格式的情況下下載圖片
  • 使用 C# 中的Image.FromStream() 函數(shù)下載不知道格式的圖片
  • 1、使用 C# 中 WebClient 類下載圖片

    WebClient 類提供了用于向 C# 中的 URL 發(fā)送數(shù)據(jù)和從 URL 接收數(shù)據(jù)的功能。
    WebClient.DownloadFile(url, path) 函數(shù)從特定的 URLurl 下載文件,并將其 保存到 path。我們可以使用 WebClient.DownloadFile() 函數(shù)從 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”) 函數(shù)從 URLurl 下載圖片并將其保存到路徑 Image.png。

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

    在上面的示例中,我們必須知道要下載的圖片文件格式;然后,我們可以從 URL 下載并保存。但是,如果我們不知道圖片文件格式,則可以使用 Bitmap 類。Bitmap 類提供了使用 C# 處理圖片的方法。該方法將以 Bitmap 類可以處理的所有格式下載和保存文件。Bitmap.Save(path, format) 函數(shù)將我們位圖的內(nèi)容寫入格式為 format 的 path 中的文件。ImageFormat 類可以在 Bitmap.Save() 函數(shù)內(nèi)部使用,以手動指定要保存的圖片格式。以下代碼示例向我們展示了如何在不使用 C# 中的 Bitmap.Save() 函數(shù)知道格式的情況下從 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() 函數(shù),該函數(shù)可以下載并保存圖片。我們使用 WebClient 類向 url 發(fā)出 Web 請求。我們使用了 Stream 類從 client.OpenRead(url) 函數(shù)中讀取數(shù)據(jù)。我們使用了 Bitmap 類將流轉(zhuǎn)換為位圖格式。然后,我們使用 bitmap.Save() 函數(shù)將 bitmap 保存在路徑 Image1.png 中,格式為 ImageFormat.Png。

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

    我們還可以使用 C# 中的 Image.FromStream() 函數(shù)來完成與上一個示例相同的操作。Image.FromStream() 函數(shù)從 C# 的內(nèi)存流中讀取圖片文件。為此,我們可以先將 URL 中的所有數(shù)據(jù)下載到字節(jié)數(shù)組中。然后,我們可以將該數(shù)組加載到 MemoryStream 類的對象中。然后,我們可以使用 Image.FromStream() 函數(shù)從 MemoryStream 類的對象讀取圖片。然后,我們可以使用 C# 中的 Image.Save(path, format) 函數(shù)將該圖片保存為特定格式的路徑。以下代碼示例向我們展示了如何在不使用 C# 中的 Image.FromStream() 函數(shù)知道其格式的情況下,從 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() 函數(shù),該函數(shù)可以下載并保存圖片。我們使用了字節(jié)數(shù)組數(shù)據(jù)來存儲 webClient.DownloadData(url) 函數(shù)返回的數(shù)據(jù)。然后,我們用 data 初始化了 MemoryStream 類的實(shí)例 mem。然后,使用 Image.FromStream(mem) 函數(shù)從 mem 中讀取圖片 yourImage。最后,通過使用 yourImage.Save(“Image2.png”, ImageFormat.Png) 函數(shù),以 ImageFormat.Png 格式將圖片 yourImage 保存到 Image2.png 路徑。

    總結(jié)

    以上是生活随笔為你收集整理的在 C# 中下载图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。