[英]Download image from img src in c#
我正在尝试从<img src="...">
下载图像。 如果src
包含文件名和扩展名,我只需将 url 传递给我的程序即可下载图像。
但是,当src
不包含文件名和扩展名时,例如
<img style="-webkit-user-select: none; cursor: zoom-in;" src="https://secure.somewebsite.net/website/member.php/1/folders/get_attach?mail_id=11111&attach_id=1">
可以从 C# 代码下载图像吗?
谢谢你。
对的,这是可能的,
您可以使用以下方法从c#下载图像:
client.DownloadFileAsync(new Uri(url), @"c:\temp\img.png");
client.DownloadFile(new Uri(url), @"c:\temp\img.png");
这是一个完整的样本
using (WebClient client = new WebClient())
{
client.DownloadFile("http://somedomain/image.png",
@"c:\temp\image.png");
// You can also you the async call
client.DownloadFileAsync("http://somedomain/image.png",
@"c:\temp\image.png");
}
现在,WebClient 已过时(-> https://learn.microsoft.com/en-us/do.net/fundamentals/syslib-diagnostics/syslib0014 )
例如使用
using var httpClient = new HttpClient();
var streamGot = await httpClient.GetStreamAsync("http://somedomain/image.png");
await using var fileStream = new FileStream("img.png", FileMode.Create, FileAccess.Write);
streamGot.CopyTo(fileStream);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.