如何将图像从mshtml.htmlimg获取到硬盘

use*_*951 11 vb.net download ihtmlimgelement

不使用API​​?

我知道有几种方法.

我顺便使用mshtml库,这比webbrowser控件更好.我正在有效地自动化Internet Explorer.

基本上我更喜欢直接拍摄图像,而不必知道htmlimg的URL并下载它.

我知道我可以从image元素中获取URL并使用webclient下载它.图像根据cookie和IP而变化.所以那不行.

我希望htmlimg元素显示的确切图像是存储的图像.

基本上好像有人正在拍摄屏幕上显示内容的本地屏幕截图.

Rob*_*ter 1

这里有一个旧的解决方案:

http://p2p.wrox.com/c/42780-mshtml-how-get-images.html#post169674

如今,您可能想查看一下 Html Agility Pack:

http://htmlagilitypack.codeplex.com/

然而,该文档并不是很好。所以这个代码片段可能会有所帮助:

HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);

// You can also load a web page by utilising WebClient and loading in the stream - use one of the htmlDoc.Load() overloads

var body = htmlDoc.DocumentNode.Descendants("body").FirstOrDefault();

foreach (var img in body.Descendants("img"))
{
    var fileUrl = img.Attributes["src"].Value;
    var localFile = @"c:\localpath\tofile.jpg";

    // Download the image using WebClient:
    using (WebClient client = new WebClient())
    {
        client.DownloadFile("fileUrl", localFile);
    }
}
Run Code Online (Sandbox Code Playgroud)