Obtaining a verification code from the WebBrowser control

Source: Internet
Author: User

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:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Mshtml;
  10. namespace getcodeimg
  11. {
  12. Public Partial class Form1: Form
  13. {
  14. Public Form1 ()
  15. {
  16. InitializeComponent ();
  17. }
  18. private void button1_click (object sender, EventArgs e)
  19. {
  20. //Open Web page via WebBrowser control
  21. WebBrowser1.Navigate (TextBox1.Text.Trim ());
  22. //Check whether the Web page has been loaded completed
  23. While (Webbrowser1.readystate < Webbrowserreadystate.complete)
  24. Application.doevents (); //If not finished loading, continue loading
  25. //Get Verification code element based on CAPTCHA ID
  26. HtmlElement Imagecodetag = webBrowser1.Document.GetElementById ("vcodesrc");
  27. //Call the Getcodeimage method to get the verification code
  28. Image finalimage = Getcodeimage (WebBrowser1, Imagecodetag);
  29. //Verification Code display
  30. pictureBox1.Image = Finalimage;
  31. }
  32. Private Image getcodeimage (WebBrowser webcontrolname, HtmlElement imgetag)
  33. {
  34. //Get all the content of the webpage
  35. HTMLDocument Hdoc = (htmldocument) WebControlName.Document.DomDocument;
  36. //Get content from the body tag of the Web page
  37. HTMLBody hbody = (HTMLBody) hdoc.body;
  38. //Create an interface
  39. Ihtmlcontrolrange HCR = (ihtmlcontrolrange) hbody.createcontrolrange ();
  40. //Get image address
  41. IHTMLControlElement himg = (ihtmlcontrolelement) imgetag.domelement;
  42. //Add a picture to the interface
  43. Hcr. Add (himg);
  44. //Copy picture to memory
  45. Hcr.execcommand ("Copy", false, null);
  46. //Get pictures from Pasteboard
  47. Image codeimage = Clipboard.getimage ();
  48. //Return the obtained verification code
  49. return codeimage;
  50. }
  51. }
  52. }



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

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.