• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2655
  • Last Modified:

How to determine xy coordinate within image

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.

  • 3
  • 2
  • 2
  • +5
1 Solution

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.
Ryan ChongCommented:

Try use GetCursorPos API to get the mouse position.

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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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.

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
Richie_SimonettiIT OperationsCommented:
azsoftAuthor Commented:
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.
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.....

Ryan ChongCommented:
See this too:

Dim WithEvents NewMenuItem As VB.Label:
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

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
Per recommendation, force-accepted.

EE Admin
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 3
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now