简体   繁体   English

dotnetbrowser-如何保存特定图像C#

[英]dotnetbrowser - how to save specific image c#

How to save in file a specific tag image of the site loaded inside the dotnetbrowser without get image from url? 如何在文件中保存dotnetbrowser内加载的站点的特定标记图像,而不从url获取图像? C#. C#。

Like: e.Browser.GetDocument().GetElementByName("imgA")).Save("C:\\aaaa.jpg"); 就像:e.Browser.GetDocument()。GetElementByName(“ imgA”))。Save(“ C:\\ aaaa.jpg”);

Based on their API documentation, GetElementByName is returning DOMElement object. 根据他们的API文档, GetElementByName返回DOMElement对象。 And there's no method called Save() inside DOMElement object. 而且DOMElement对象中没有称为Save()方法。

Assume I have html like this: 假设我有这样的html:

 <!DOCTYPE html> <html> <body> <img src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" width="500" height="333"> </body> </html> 

So first, you need to get TextContent (which is image url) from DOMElement 因此,首先,您需要从DOMElement获取TextContent (即图像网址)

var url = e.Browser.GetDocument().GetElementByName("img").GetAttribute("src");

Then you can load image url into Browser. 然后,您可以将图片网址加载到浏览器中。 To download file using DotNetBrowser you should create DownloadHandler instance. 要使用DotNetBrowser下载文件,您应该创建DownloadHandler实例。 Here's example how to download file using DotNetBrowser: https://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000110026-file-downloads 以下是使用DotNetBrowser下载文件的示例: https ://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000110026-file-downloads


However personally i'd prefer to use WebClient instead of DotNetBrowser DownloadHandler. 但是我个人更喜欢使用WebClient而不是DotNetBrowser DownloadHandler. So you can download image from url like this: 因此,您可以像这样从url下载图像:

using (var client = new WebClient()) client.DownloadFile(url, "C:\\aaaa.jpg");

More about WebClient.DownloadFile: https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadfile?view=netframework-4.7.2 有关WebClient.DownloadFile:更多信息WebClient.DownloadFile: https WebClient.DownloadFile: ? WebClient.DownloadFile: = WebClient.DownloadFile:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM