首页 > > > c# 如何从webbrowser获取图片?
我需要获取图片到变量中,网上有以下方法,但无法满足我的需求。因为使用到了剪切板,因为我可能有大量数据多个实例在run,可能会遇到并发访问剪切板的情况,会出错。
请问有没有更好的实现或可以锁定异步访问剪切板也可以?
/// <summary> /// 返回指定WebBrowser中图片<IMG></IMG>中的图内容 /// </summary>
/// <param name="WebCtl">WebBrowser控件</param>
/// <param name="ImgeTag">IMG元素</param>
/// <returns>IMG对象</returns>
private Image GetWebImage(WebBrowser WebCtl, HtmlElement ImgeTag) {
HTMLDocument doc = (HTMLDocument)WebCtl.Document.DomDocument;
HTMLBody body = (HTMLBody)doc.body;
IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange();
IHTMLControlElement Img = (IHTMLControlElement)ImgeTag.DomElement; //图片地址
Image oldImage = Clipboard.GetImage();
rang.add(Img);
rang.execCommand("Copy", false, null); //拷贝到内存
Image numImage = Clipboard.GetImage();
try
{
Clipboard.SetImage(oldImage);
}
catch {}
return numImage;
}来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2618/viewspace-3170607/ ,如需转载,请注明出处,否则将追究法律责任。