如何从URL中使用C#下载图片 [英] How to download image from url using c#

查看:532
本文介绍了如何从URL中使用C#下载图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有是有直接从C#中的URL下载图像如果URL没有在链路的最后一个图像格式的方式。网址示例:

<$p$p><$c$c>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_110e04e71d969d392b63b27ec4f4b24a

我知道如何下载图像如果URL以图像格式结束。例如:

<$p$p><$c$c>http://img1.wikia.nocookie.net/__cb20101219155130/uncyclopedia/images/7/70/Facebooklogin.png


解决方案

您可以使用下面的方法。

 使用(Web客户端的客户端=新的WebClient())
  {
    client.DownloadFileAsync(新的URI(URL),@C:\\ TEMP \\ image35.png);
    client.DownloadFile(新的URI(URL),@C:\\ TEMP \\ image35.png);
   }

这些方法如下DownloadString(..)和DownloadStringAsync(...)几乎相同。他们Directory中的文件存储,而不是在C#中的字符串,不需要在URI格式,扩展

There is has the way to download image directly from url in c# if the url does not have an image format at the end of the link. Example of url:

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_110e04e71d969d392b63b27ec4f4b24a

I knew how to download the image if the url end with image format. Eg:

http://img1.wikia.nocookie.net/__cb20101219155130/uncyclopedia/images/7/70/Facebooklogin.png

解决方案

You can use Following methods.

  using (WebClient client = new WebClient()) 
  {
    client.DownloadFileAsync(new Uri(url), @"c:\temp\image35.png");
    client.DownloadFile(new Uri(url), @"c:\temp\image35.png");
   }

These methods are almost same as DownloadString(..) and DownloadStringAsync(...). They store the file in Directory rather than in C# string and no need of Format extension in URi

这篇关于如何从URL中使用C#下载图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆