1

I want to get the coordinates(X,Y) of 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,

1
  • 2
    what are you trying to solve? you are giving a solution not a problem.
    – Vitalik
    Apr 25, 2011 at 13:20

4 Answers 4

1

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.

2
  • do jquery support template matching.means i have image which coordinates i want to find in webpage.
    – Zain Ali
    Apr 17, 2011 at 12:33
  • Yes, you can give that image an ID or a class name. For example: var x = $('your-image-id').offset().left; var y = $('your-image-id').offset().top; Apr 17, 2011 at 12:39
1

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.

0

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.

1
  • no i have stored image locally and i want to find this image location in webpage(template matching).
    – Zain Ali
    Apr 17, 2011 at 12:38
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.