[英]Download image from img src in c#
I am trying to download image from <img src="...">
.我正在尝试从<img src="...">
下载图像。 If the src
contains the file name and extension, I can download image simply passing url to my program.如果src
包含文件名和扩展名,我只需将 url 传递给我的程序即可下载图像。
However, when the src
does not contains file name and extension, like但是,当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">
it is possible to download the image from C# code?可以从 C# 代码下载图像吗?
Thank you.谢谢你。
Yes it is possible, 对的,这是可能的,
You can use these methods to download the image from c# : 您可以使用以下方法从c#下载图像:
client.DownloadFileAsync(new Uri(url), @"c:\temp\img.png");
client.DownloadFile(new Uri(url), @"c:\temp\img.png");
Here is a complete sample 这是一个完整的样本
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");
}
Now, WebClient is obselete (-> https://learn.microsoft.com/en-us/do.net/fundamentals/syslib-diagnostics/syslib0014 )现在,WebClient 已过时(-> https://learn.microsoft.com/en-us/do.net/fundamentals/syslib-diagnostics/syslib0014 )
use for exemple例如使用
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.