0

I am using DotNetBrowser Control in my c# desktop application. I am not able to get captcha image using this.

It is easy in Webbrowser control but in DotNetBrowser I don't know how to do so in c#

https://dotnetbrowser.support.teamdev.com/support/solutions/9000111998

This code is working if I am using Webbrowser control

private Image getCaptcha()
{
    HtmlElement ement = webBrowser1.Document.GetElementById("imgCaptcha");
    if (ement == null)
    {
        return null;
    }
    mshtml.HTMLWindow2 w2 = (mshtml.HTMLWindow2)webBrowser1.Document.Window.DomWindow;
    w2.execScript("var ctrlRange = document.body.createControlRange();
    ctrlRange.add(document.getElementById('imgCaptcha'));
    ctrlRange.execCommand('Copy');", "javascript");
    return Clipboard.GetImage();
}

I need similar code in DotNetBrowser control

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

0

You can use the 'Browser.ImageProvider.GetImage' method to get the screenshot of the page and then crop this image to the bounds of the captcha image. The only restriction is that the Browser should use the lightweight rendering mode as getting an image is unavailable in the heavyweight mode.

The described approach may look like the following source code:

browserView = new WinFormsBrowserView(BrowserFactory.Create(BrowserType.LIGHTWEIGHT));
//...
browserView.Browser.SetSize(1024, 768); 
Bitmap screenshot = browserView.Browser.ImageProvider.GetImage() as Bitmap; 
DOMElement captchaElement = browserView.Browser.GetDocument().GetElementById("imgCaptcha"); 
pictureBox1.Image = screenshot?.Clone(captchaElement.BoundingClientRect, screenshot.PixelFormat);
Eugene Yakush
  • 259
  • 4
  • 6