Solved

How to determine xy coordinate within image

Posted on 2002-06-28
12
2,620 Views
Last Modified: 2007-11-27
hi,
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.

0
Comment
Question by:azsoft
  • 3
  • 2
  • 2
  • +5
12 Comments
 
LVL 6

Accepted Solution

by:
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.
Marco
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 7115753
Hi,

Try use GetCursorPos API to get the mouse position.

http://www.allapi.net/apilist/apifunction.php?apifunction=GetCursorPos
0
 
LVL 17

Expert Comment

by:inthedark
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
0
 
LVL 17

Expert Comment

by:inthedark
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.

0
 
LVL 6

Expert Comment

by:pierrecampe
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
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7116364
Hearing...
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:azsoft
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.
0
 
LVL 17

Expert Comment

by:inthedark
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
            MouseSet
            frm.Show
            Exit Sub
        End If
    End If
Next bc

Notes:

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.

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

etc......

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"
else
   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.....









0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 7121331
See this too:

Dim WithEvents NewMenuItem As VB.Label:
http://www.experts-exchange.com/visualbasic/Q_20317773.html
0
 
LVL 6

Expert Comment

by:pierrecampe
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)
    label1(i).visible=true
    label1(i).move,x,y,neededwidth,neededhight
    label1(i).tooltiptext="thebuildingname"
next
and in:
Private Sub Label1_Click(Index As Integer)
        select case index
               case 0
                    msgbox "this is building0"
               case 1
                    msgbox "this is building1"
               'etc....
        end select

End Sub


 
0
 
LVL 49

Expert Comment

by:DanRollins
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
0
 
LVL 5

Expert Comment

by:Netminder
ID: 8022542
Per recommendation, force-accepted.

Netminder
EE Admin
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now