1, first introduce the basic control (drag control everyone will, I do not introduce each), see;
2, add Mshtml Reference, the steps are as follows:
Solution-Right-click "References"-? Add Reference-Locate the microsoft.mshtml component under. NET-click OK;
3. Write the code, double-click the Button1 button to add the event; (note the using MSHTML in the reference class;)
Target website: http://admin.3cjz.cn/reg.php?username=ghdhdd
Full code:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using Mshtml;
- namespace getcodeimg
- {
- Public Partial class Form1: Form
- {
- Public Form1 ()
- {
- InitializeComponent ();
- }
- private void button1_click (object sender, EventArgs e)
- {
- //Open Web page via WebBrowser control
- WebBrowser1.Navigate (TextBox1.Text.Trim ());
- //Check whether the Web page has been loaded completed
- While (Webbrowser1.readystate < Webbrowserreadystate.complete)
- Application.doevents (); //If not finished loading, continue loading
- //Get Verification code element based on CAPTCHA ID
- HtmlElement Imagecodetag = webBrowser1.Document.GetElementById ("vcodesrc");
- //Call the Getcodeimage method to get the verification code
- Image finalimage = Getcodeimage (WebBrowser1, Imagecodetag);
- //Verification Code display
- pictureBox1.Image = Finalimage;
- }
- Private Image getcodeimage (WebBrowser webcontrolname, HtmlElement imgetag)
- {
- //Get all the content of the webpage
- HTMLDocument Hdoc = (htmldocument) WebControlName.Document.DomDocument;
- //Get content from the body tag of the Web page
- HTMLBody hbody = (HTMLBody) hdoc.body;
- //Create an interface
- Ihtmlcontrolrange HCR = (ihtmlcontrolrange) hbody.createcontrolrange ();
- //Get image address
- IHTMLControlElement himg = (ihtmlcontrolelement) imgetag.domelement;
- //Add a picture to the interface
- Hcr. Add (himg);
- //Copy picture to memory
- Hcr.execcommand ("Copy", false, null);
- //Get pictures from Pasteboard
- Image codeimage = Clipboard.getimage ();
- //Return the obtained verification code
- return codeimage;
- }
- }
- }
4. Verification procedure
PS: I am still a rookie-level program ape, if you write a mistake, welcome correction.
Obtaining a verification code from the WebBrowser control