开发者

Template matching in C#

I want to get the coordinates(X,Y) o开发者_开发问答f specific image in a web page.After getting that image coordinates I shall move the cursor to this position and click programmatically over this image.

I have saved the image locally I have to compare I will compare this image with the web page and find its location in web page. how is this possible in C#

if this is not possible in C#. Will anybody suggest me any hack in this scenario,


Any webpage is client's browser specific. You can't do that in C# since C# is a server language and runs at server and then returns the results in browser specific language. Each client will have different resolution and hence different position for the image. In order to get the coordinates of the image you'll need to use javascript and then move your pointer to that location. You can use jQuery and get image position.


Template Matching in AForge is done with the ExhaustiveTemplateMatch class:

http://www.aforgenet.com/framework/docs/html/17494328-ef0c-dc83-1bc3-907b7b75039f.htm

But you would first need the screenshot of the browser window to match to.


As I understand you want to simulate a click event on a browser that displays a page containing your image. If so, you can do somthing like this -

IntPtr lParam = (IntPtr)((y << 16) | x); 
IntPtr wParam = IntPtr.Zero; // Modifier flags - eg ctrl etc 
SendMessage(handle, 0x201, wParam, lParam); // Mouse LBUTTON down
SendMessage(handle, 0x202, wParam, lParam); // Mouse LBUTTON up

handle is target browser window handle, and (x, y) is the location where click is to be simulated - can be the center of your image rect.


I would use plain JavaScript to get the information from the web page.

function getX(/* string */ elementId) {
    return document.getElementById(elementId).offsetLeft;
}

function getY(/* string */ elementId) {
    return document.getElementById(elementId).offsetTop;
}

If you need the position relative to the viewport:

function getX(/* string */ elementId) {
    return document.getElementById(elementId).offsetLeft - document.body.scrollLeft;
}

function getY(/* string */ elementId) {
    return document.getElementById(elementId).offsetTop - document.body.scrollTop;
}

If you need to perform the onClick action on the image without using the mouse:

function clickIt(/* string */ elementId) {
    document.getElementById(elementId).onclick();
}

To invoke JavaScript using C#, you'll need to create a WebBrowser control, load the web page, then invoke the script. You can find how to do that here.

MSDN documentation for HtmlDocument.InvokeScript

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜