如何从 WebBrowser 控件获取图像 [英] How to get image from WebBrowser control

查看:39
本文介绍了如何从 WebBrowser 控件获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WebBrowser 控件,在导航页面后我需要下载图像.我使用了以下代码:

I have a WebBrowser control, after navigating a page I need to download the image. I used the following code:

HtmlElementCollection tagsColl = webBrowser1.Document.GetElementsByTagName("img");
foreach (HtmlElement currentTag in tagsColl)
{
     ...
     using (var client = new WebClient())
     {
           ...
           client.DownloadFile(currentTag.GetAttribute("src"), path);
           ...
     }
}

但是,在这种情况下,webclient 启动了一个新会话,并且新会话中的链接不正确.我需要在与 webbrowser 相同的会话中执行此操作,只有在这种情况下,我才能获得正确的图像链接.

but, in this case webclient starts a new session, and link in new session is not correct. I need to do this in same session as webbrowser, only in this case i get a correct link to the image.

我该怎么做?

推荐答案

尝试使用 URLDownloadToFile,它应该为您提供与 WebBrowser 使用的相同的会话和缓存.

Try downloading the image using URLDownloadToFile, which should give you the same session and cache as used by WebBrowser.

这篇关于如何从 WebBrowser 控件获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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