如何从网址下载图片

如何从网址下载图片,第1张

如何从网址下载图片

只需 使用以下方法即可。

using (WebClient client = new WebClient()) {    client.DownloadFile(new Uri(url), @"c:tempimage35.png");    // OR     client.DownloadFileAsync(new Uri(url), @"c:tempimage35.png");}

这些方法与DownloadString(..)和DownloadStringAsync(…)几乎相同。他们将文件存储在目录中,而不是C#字符串中,并且不需要URi中的格式扩展名

如果您不知道图像的格式(.png,.jpeg等)
public void SaveImage(string filename, ImageFormat format){        WebClient client = new WebClient();    Stream stream = client.OpenRead(imageUrl);    Bitmap bitmap;  bitmap = new Bitmap(stream);    if (bitmap != null)    {        bitmap.Save(filename, format);    }    stream.Flush();    stream.Close();    client.Dispose();}
使用它
try{    SaveImage("--- Any Image Path ---", ImageFormat.Png)}catch(ExternalException){    // Something is wrong with Format -- Maybe required Format is not     // applicable here}catch(ArgumentNullException){       // Something wrong with Stream}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5113691.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-17
下一篇2022-11-17

发表评论

登录后才能评论

评论列表(0条)

    保存