C # Some experiences using Webbrowser

Source: Internet
Author: User
1. How to obtain "webpage opening error" Information

 

In the DocumentCompleted event, you can determine the "res: //": flag in Document. Url. AbsoluteUri. (In the past, e. Url was used. No wonder it cannot be intercepted)

If (webBrowser1.Document. Url. AbsoluteUri. IndexOf ("res: //")>-1) // error handling

{

WebBrowser1.Navigate (e. Url );

Return;

}

2. How to Use MSHTML functions such as IHTMLDocument2

VS2005 does not fully encapsulate MSHTML functions, leaving a DomDocument interface. After you directly reference the Microsoft HTML Object Library Class Library, you can operate on complex functions such as IHTMLDocument2. For example, IHTMLDocument2 doc2 = (IHTMLDocument2) webBrowser1.Document. DomDocument;

3. How to extract images from webpages, especially those returned by streams, such as verification code Images

Some images of many websites are dynamically generated. They are sent from the server in a stream mode and then assembled into images. Whatever the method, the client is complete. The advantage of using WebBrowser is here, as long as the final result is managed, it will be OK. The following code obtains the verification code on the webpage:

/// <Summary>

/// Return the image content in the specified WebBrowser </IMG>.

/// </Summary>

/// <Param name = "WebCtl"> WebBrowser Control </param>

/// <Param name = "ImgeTag"> IMG element </param>

/// <Returns> IMG object </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 address

Image oldImage = Clipboard. GetImage ();

Rang. add (Img );

Rang.exe cCommand ("Copy", false, null); // Copy to memory

Image numImage = Clipboard. GetImage (); // obtain the graph from the Clipboard

Clipboard. SetImage (oldImage); // restore

Return numImage;

}

4. How to shield the Alert () pop-up window

I declare that this is not a technology, but a processing technique. I checked a lot of data on the Internet and it is difficult to block the Alert () window displayed on the webpage (especially from Writer ). I use two extractors, one as the main program. exe, and the other as run.exe. The webbrowsercontrol is placed in run.exe, and run.exeis called through Processin in the main program. After the task is completed, run.exe is killed (Kill). Then, the Alert window is automatically closed. I used this technique to build a voting machine, which can run quietly, avoid Session processing and other problems, and leave no window on the desktop.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.