Take screenshot of div in InternetExplorer - c#

I am using mshtml.InternetExplorer object within my winforms application to browse a web application. Somehow I can't use webbrowser in my solution. Now I need to capture screenshot of a div element inside this web application. I see lots of solutions doing similar functionality using webbrowser's drawtobitmap method but none is using mshtml.InternetExplorer.
Is there way to achieve this functionality ? Is there some way that I can type cast InternetExplorer object into webbrowser?

You should be able to capture the desired part of the main IE window using PrintWindow Windows API, as explained here. You can get the handle of the window by calling IWebBrowser2::HWND on the IE object. Some other methods could also help to make this happen:
IWebBrowser2::ClientToWindow
IHTMLElement::scrollIntoView
IHTMLElement2::getBoundingClientRect

Related

Chromium wrapped in WPF - find element and invoke action

ChromiumWebBrowser is wrapped inside WPF Windows app. https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Wpf/ChromiumWebBrowser.cs
When I use Inspect tool for Windows I am not able to locate elements inside Chromium. Seems to be image without any name. Is it possible to invoke action in Chromium any way?
I think this is an accessibility / UIA issue.
By default Chromium doesn't enable accessibility unless it detects a screen reader or other advanced use of the accessibility APIs.
With the standalone browser you can enable it manually by using the --force-renderer-accessibility command-line flag, or visit chrome://accessibility to enable it for one session.
For your issue with CefSharp.Wpf ChromiumWebBrowser things get more complicated, see these:
How to make JAWS screen reader recognize and read content of cefsharp ChromiumWebBrowser control?
https://github.com/cefsharp/CefSharp/issues/2053

WebBrowser not loading youtube tv

i have the following URL that doesnt behave in the WPF WebBrowser control as expected. It basically loads only a youtube background image and then stops .. where as firefox/chrome etc. load the youtube view you get on tv-apps.
is there a way to make the WebBrowser load this in the same way?
https://www.youtube.com/tv#/browse-sets?c=UCZGYJFUizSax-yElQaFDp5Q&resume
i tried to parse the same page downloaded with a webclient.. but its also only loading an background page and i dont know how to continue from here.
thanks in advance for any hints
Wpf WebBrowser is a wrapper for IE. If you try to open the link you provided with IE (11 in my case) you can see that it won't work (not supported).
If you change the version of IE that your WebBrowser use (as i described here Script Error in webBrowser control WPF) you can see the not supported page.
You can try using something like https://bitbucket.org/geckofx/ or https://github.com/cefsharp/CefSharp

How to use web browser control in the browser?

I'm trying to load a HTML page via the web browser control in my Silverlight project, but it seems that the content of the control can only be viewed from out-of-browser mode.
Is there in way to use it in the browser?
Could you give more detail? if you are wanting to control and interact with the WebBrowser Control in the Internet Explorer window, then you can do this. The code will remain the same, except you will need to change your MSHTML DOM reference when declaring your (say) HTML Document, and it'll be declared as an InternetExplorer type (comes up in intellisense after the "As" part if you're using VB, same story with c#).
Once you reference this window, you can control it - everything is the same since IE uses the WB control as well, if you however want to use the WB control in your app, you should be able to, give us some more details, are you saying that the content or links are opening up in a new external IE window? If that is the case, these things are fixable :).
Also, if you're using WPF .NET I think there is a different, less cooler WB control for that than the traditional .NET WB control. Please elaborate on your question so I can help you further.

Attach Watin to WebBrowser (WPF)

I've scoured the internet (including answers from this site) but nothing appears to work for me. Does anyone know the correct approach to attach WatiN to a WebBrowser instance (Windows Presentation Forms version)?
Most of the answers I've read tell me about 'ActiveXInstance', which is not visible in the WPF version of WebBrowser. I've tried exposing the IWebBrowser 2 interface from the WebBrowser documentation (http://msdn.microsoft.com/en-us/library/cc491073%28v=VS.90%29.aspx) but after much reworking of the code structure to get it to compile, a simple WatiN goto(URL statement simply ends up timing out.
As a last resort I've tried
WatiN.Core.Settings.AutoStartDialogWatcher = false;
var browser = new IE(wbrowser);
On the loadcompleted event (as the browser instance will result in null if I place it in the MainWindow() constructor method), but that brings up an Argument Exception with the message "iwebBrowser2 needs to implement shdocvw.IWebBrowser2". Unfortunately I've no idea how to resolve this.
It's likely that you'll want to attach by window handle:
var ie = IE.AttachToIE(Find.By("hwnd", containerHwnd);
In the WatiN_IE_ExtensionMethods.cs API I wrote for O2, I was able to get it to work quite nicely with the normal WinForms webbrowser (inside another WinForm control or an WPF control).
Since you can use WinForm controls inside WPF (and that is exactly what .NET 3.5 is doing with the WPF WebBrowser since it is not a native WPF control), here is an example that: does exactly that Using WatiN to inside WPF.
This video shows an O2 script that uses this technique: http://www.youtube.com/watch?v=YsVX5-nGHWI
Note that I wrote a bunch of Extension methods to make WatiN easier to consume:
var ie = winFormsPanel.add_IE();
ie.open("http://www.google.com");
ie.link("Videos").flash().click();
ie.field("q").value("OWASP O2 Platform").flash();
ie.button("Search Videos").flash().click();
ie.link("O2 Platform - XSS PoC builder.avi").scrollIntoView().flash().click();

How to use C# to capture a image of a specific url?

How to use C# to capture a image of a specific url?
I want to use C# to automatically capture a image of a webpage based on a specific url.
For example, I have a page contains a txtUrl.Text = "http://www.some.com/index.aspx" , then I click a button, how can I capture a image of that Url?
I assume you want to do this from ASP.NET (as opposed to from a WinForms application).
In your web project, add a reference to System.Windows.Forms (yes, this is a bad thing to do). In your code-behind, you can then create an object of type System.Windows.Forms.WebBrowser:
WebBrowser browser = new WebBrowser();
// this will load up a URL into the web browser:
browser.Navigate(#"http://www.stackoverflow.com");
Next, just use the BitBlt API function (sorry, I don't have a link handy) to copy the WebBrowser control's graphical display to a Bitmap (which you can then display or save or whatever). With this function, the WebBrowser's Handle property is one of the parameters to pass.
Update: here's a link to some code that does exactly what you need: http://www.developerfusion.com/code/4712/generate-an-image-of-a-web-page/
If you mean a visual of the webpage, one approach is to integrate IE to your application and programmatically taking a screenshot. This (for the integrated web browser) and this (for taking screenshots with C#) may be of use. This is of course IE dependent.
Another option is using the shotserver and shotfactory projects used for browsershots.org. They can be found here, though I'm not sure if there's a .NET API for it.
I don't think that is really possible only using C#. That is because C#, or the .NET framework for that matter, don't offer any kind of HTML markup rendering capabilities. The closest you can get - in my opinion - would be to use a WebBrowser control and then try to somehow capture it's graphical output (which would be the rendered page).
The other way to do it would be to look for a .NET component that might do what you want.. Although I don't know of any that do.

Categories