How to determine xy coordinate within image

Posted on 2002-06-28
Last Modified: 2007-11-27
i load an image into form by loadpicture. after the image has been loaded, i want to determine the xy coordinate of the location within the image where i point the mouse. how can i do that.

the image is a map. with the coordinate i can link with my database to describe details of what i point to. Example when i point to a building in the map, base on the coordinate my program will read databse and return the building details.

Question by:azsoft
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +5

Accepted Solution

marconovaro earned 50 total points
ID: 7115752

Just use the mouseMove event:

(e.g. for a PictureBox)

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

the X and Y parameters tell you the position of the mouse related to the upper-left corner of the picturebox itself.
LVL 52

Expert Comment

by:Ryan Chong
ID: 7115753

Try use GetCursorPos API to get the mouse position.
LVL 17

Expert Comment

ID: 7115833
The answer is simple, no need for API calls, try this:

Option Explicit
Dim StoreX
Dim StoreY
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    StoreX = X
    StoreY = Y
End Sub

Private Sub Form_Click()
    'Now you hace you co-ordinates in
    'StoreX and StoreY
End Sub
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

LVL 17

Expert Comment

ID: 7115840
Take my advice when working with graphics ALWAYS set your form scalemode to use pixels. But be warned there is a bug in VB some of the mousemove/drag & drop co-ordinates  should return pixels but sometimes they return twips so always check and convert if necessary.


Expert Comment

ID: 7115927
as the experts have told you the mouse events give you the coordinates of the mouse
to know whats under the mouse you need the coordinates of the contour of every building on your form and determine if the x,y fals within that surface
very powerfull and very difficult to implement
but maybe you can determine what building the mouse is on by its color
if so its easier to use the point (x,y) method wich gives you the color under the mouse
easier still and what i usually do for such things, is make 'hotspots' on the form
a hotspot is simply a transparent label
a label has all the events you need,and as an added bonus it has a tooltiptext property so you can even show a small description when the mouse moves over it
LVL 16

Expert Comment

ID: 7116364

Author Comment

ID: 7117847
Hotpots using label is a good idea and it will simplified the coding.
but how can i load a label onto image during runtime not during design.
LVL 17

Expert Comment

ID: 7118164
The coding to find where you are is not so hard.  I created a marina system where the berths were on a map. To find a location simply loop through an array.

I used a UDT (user defined type) to fold the coordinates.  This is where I discovered that twips are a problem.

' Shared Berth Details
Public Type BerthDef
    Top As Single
    Left As Single
    Width As Single
    Height As Single
    item As Long ' record number in database
    MainID as Long
End Type
Global Berths() As BerthDef
Global ActualPosBerths() As BerthDef
Global BerthsCount As Long

The click routine went like this:

cberth = -1
For bc = 0 To BerthsCount
    If MouseX >= Berths(bc).Left And MouseX <= Berths(bc).Left + Berths(bc).Width Then
        If MouseY >= Berths(bc).Top And MouseY <= Berths(bc).Top + Berths(bc).Height Then
            ' They clicked on a berth
            ' launch form to display the Berth data
            cberth = bc
            Dim frm As Form
            Set frm = New frmBerth
            frm.pBerth = Berths(bc).id
            Exit Sub
        End If
    End If
Next bc


1) You need to hold 2 copies of the co-ordinates, 1 is loaded from a database and the other is set-up in the form_resize event.  So that as your picture is resized you chane the ordinates by proprtion of the full image to the displayed image.

Factor = CurrentWidth/LoadedWidth
For bc = 0 To BerthsCount
    Berths(bc).Width=ActualPosBerths(bc).Width * Factor


2) The co-ordinates of each object may be defined more than once becuase it may go round corneres.

I used a mousedown and mouseup and the mousemove events to aquire a new object.  I drew the object with 4 line objects.

So the mousedown recorder the start position and set an indicator Mousedown=True, if the mousemove detected the mouse was down the lines could be displayed.  I also used drag & drop to copy similar objects.

3) In my case the objects were simplefied because they were running only north and south but in your case this may be different. An object could be made from a series of boxes that have been squished over parallelogram or tapeezoid. So you need to extend your click routine as follows:

First check that the mouse is between the upper and lower co-ordiantes and beweet the right and right coordinates this only takes just a split second (as shown in the previous example.

You then need to check the gradient of the vertical lines and make sure the the click is on or to the right of the left line and on or to the left of the left line.

So if topleft coordinates are tx and ty and the bottom left coordiantes are bx and by:

Height of object is:

h = ty - by

The line offset is from north south is:

oset = tx - bx

So did they click to the right of the line?

So the gradient of the line is:

g = h / oset

Now calculate the gradient of the mouse click

hm = mousey - by
moset = mousex - bx

mg = hm / mouset

if mg >= G then
   msgbox "Clicked on the line or to the right"
   msgbox "they did not click in the squished box"
end if

So you now ned to use the same principle to check the right edge.

Hope this helps,

still inthedark.....

LVL 52

Expert Comment

by:Ryan Chong
ID: 7121331
See this too:

Dim WithEvents NewMenuItem As VB.Label:

Expert Comment

ID: 7121700
or just place a label1 on your form at design time and give it index 0
and at runtime:
for i=1 to numberoflabelsneeded
    load label1(i)
and in:
Private Sub Label1_Click(Index As Integer)
        select case index
               case 0
                    msgbox "this is building0"
               case 1
                    msgbox "this is building1"
        end select

End Sub

LVL 49

Expert Comment

ID: 7964660
Hi azsoft,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept marconovaro's comment(s) as an answer.

azsoft, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer

Expert Comment

ID: 8022542
Per recommendation, force-accepted.

EE Admin

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question