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

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

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

C#

C#中使用WebClient下载指定url的网络照片

發(fā)布時(shí)間:2025/3/19 C# 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#中使用WebClient下载指定url的网络照片 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

場(chǎng)景

效果

?

方法

DownloadFile(Uri, String)
將具有指定 URI 的資源下載到本地文件。

public void DownloadFile (Uri address, string fileName);
參數(shù)
address
Uri
以 String 形式指定的 URI,將從中下載數(shù)據(jù)。
fileName
String
要接收數(shù)據(jù)的本地文件的名稱(chēng)。


官方文檔

https://docs.microsoft.com/zh-cn/dotnet/api/system.net.webclient.downloadfile?redirectedfrom=MSDN&view=netframework-4.8#overloads

實(shí)現(xiàn)

構(gòu)建圖片下載url

這里本地運(yùn)行Tomcat后,構(gòu)建圖片的url

http://localhost:8080/tomcat.png

?

這樣就可以訪(fǎng)問(wèn)本地實(shí)現(xiàn)圖片的url了。

自己可以另行尋找url。

實(shí)現(xiàn)

設(shè)計(jì)窗體如下

?

在按鈕的點(diǎn)擊事件中編寫(xiě):

?private void btnDownload_Click(object sender, EventArgs e){try{WebClient webClient = new WebClient();//判斷文件是否存在if (!File.Exists("dingdang.png")){//下載文件webClient.DownloadFile("http://localhost:8080/tomcat.png", "dingdang.png");}//將指定路徑的圖片顯示在窗體中picShow.Image = Image.FromFile("dingdang.png");}catch(Exception ex){MessageBox.Show(ex.ToString());}}

完整代碼

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.IO;namespace WebClientDemo {public partial class WebClientDemoForm : Form{public WebClientDemoForm(){InitializeComponent();}private void btnDownload_Click(object sender, EventArgs e){try{WebClient webClient = new WebClient();//判斷文件是否存在if (!File.Exists("dingdang.png")){//下載文件webClient.DownloadFile("http://localhost:8080/tomcat.png", "dingdang.png");}//將指定路徑的圖片顯示在窗體中picShow.Image = Image.FromFile("dingdang.png");}catch(Exception ex){MessageBox.Show(ex.ToString());}}} }

示例代碼下載

https://download.csdn.net/download/badao_liumang_qizhi/11545922

總結(jié)

以上是生活随笔為你收集整理的C#中使用WebClient下载指定url的网络照片的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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