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.
The first thing to analyze is what the friend is going to get. Validation picture element characteristics:
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.
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 ";
var b =newbitmap (F.clientrectangle.width, f.clientrectangle.height);
Wb. DrawToBitmap (b, Newrectangle (Newpoint (), f.clientrectangle.size));
pictureBox1.Image = b;
Compile and run to test:
Click the button, wait a moment, you can display the complete code picture in the PictureBox.
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.