WebBrowser image of the verification Code on the Intercept page

Source: Internet
Author: User

Introduction

Recently, with a friend to get WebBrowser access to the Web page to verify the image of the method, the first thought is to download the current page through the WebClient of the reference to the verification code picture, but then think that this will involve some problems: one is that the verification code may be changed at each request, Second, it is difficult to associate WebClient with WebBrowser cookies and even sessions.

Then think of can give up WebBrowser, always use the WebRequest for the lower level of access, to avoid multiple access to the verification code to produce changes resulting in inconsistencies, but this approach is still more complex to operate.

And then think of this alternative method--grasping the picture. You just need to go through the WebBrowser screenshots and cut off the parts beyond the verification code.

Ideas

The first thing to analyze is what the friend is going to get. Validation picture element characteristics:

URL: Http://www.jcard.cn/Charge/UCardDirectCharge.aspx?category=AAWYVVWYKV&product=AAWYVVWYKV010CV

It's very simple, we just find the src attribute "randomimage.aspx" the end of the picture, we find the element we need.

After this element is found, in order to conveniently determine its position and ensure that it does not exceed the WebBrowser visual range, we give it an inline style that is in the absolute upper-left corner of the page and set Z-index to 9999 to avoid being overwritten by other elements, which is:

"Position:absolute; z-index:9999; top:0px; left:0px "

After this can be done through the WebBrowser DrawToBitmap method screenshots, screenshots and the height of the above elements can be achieved through the ClientRectangle properties.

Realize

So now set up a WinForm project to do the test, design the following interface:

Then write the event handler function for the button:

Privatevoid Button1_Click (object sender, EventArgs e)

{

var wb =newwebbrowser ();

Wb. Navigate ("Http://www.jcard.cn/Charge/UCardDirectCharge.aspx?category=AAWYVVWYKV&product=AAWYVVWYKV010CV");

Wait for load to complete

while (WB. ReadyState

Traversal find the element where the image is validated

foreach (HtmlElement f in WB.) Document.images)

{

if (F.getattribute ("src"). ToLower (). EndsWith ("randomimage.aspx"))

{

Absolutely position the element to the upper-left corner of the page

F.style = "Position:absolute; z-index:9999; top:0px; left:0px ";

Catch picture

var b =newbitmap (F.clientrectangle.width, f.clientrectangle.height);

Wb. DrawToBitmap (b, Newrectangle (Newpoint (), f.clientrectangle.size));

pictureBox1.Image = b;

Break

}

}

}

Compile and run to test:

Click the button, wait a moment, you can display the complete code picture in the PictureBox.

Tips

WebBrowser's DrawToBitmap method is hidden, is not supported by IntelliSense prompts, I do not know why this, but I know DrawToBitmap method is really a problem, is a screenshot of the whole white phenomenon, according to my observation, This behavior occurs and whether the WebBrowser control is displayed, as long as the WebBrowser control is displayed on the form, the screenshot is full white, and if the WebBrowser is not loaded into the form, the screenshot is normal, the specific reason is unknown, Can only sigh again webbrowser this powerful control writing is too coarse.

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.