I'm having a bit of a problem with coordinates and mapping PictureBoxes. Here's the situation:

I've got a PictureBox holding an image of a football pitch. The aim is to record the shot positions on this pitch. So whenever the mouse is clicked on the pitch, I am recording the X and Y coordinates of the mouse at that time. This means that when I read from the database, it plots the shots accordingly.
The problem I am facing is when the form is moved from the top left corner of the screen.
I have the form set at 1024x768px - a fixed size. If the form is moved from the left corner of the screen, the shots are placed wrongly due to the coordinates changing.

I understand that the code that I am using is getting me the screen coordinates:

XPos = Control.MousePosition.X - 6
YPos = Control.MousePosition.Y - 27

What I am looking for is a way to get the coordinates relative to the form itself. This would mean that anytime I pull the coordinates from the database, they'll be correct no matter what the form's position is on the screen.

Any help would be appreciated.

Dani AI

Generated

A short, practical summary that ties the answers together and fills the gaps left by the thread:

described the classic symptom of using screen coordinates and hard-coded offsets — markers move when the form moves. 's suggestion to convert to control/client coordinates is the right direction, and 's idea about scaling is useful, but two important pieces were missing: (1) use the PictureBox's mouse event (or convert to the PictureBox client point) rather than subtracting magic numbers, and (2) convert between the PictureBox display rectangle and the image's native pixels (especially when SizeMode is Zoom or StretchImage). The robust approach is to save coordinates in image-space or as normalized fractions (0..1) and to reproject those to the PictureBox display rectangle when drawing.

The code below demonstrates a helper that converts a click point (control coordinates) into image pixel coordinates for common SizeMode values, and a second helper that converts normalized image coordinates back to display points for painting markers.

' Map a point from PictureBox client coords to image pixel coords
Function GetImagePoint(pb As PictureBox, clickPt As Point) As PointF
    If pb.Image Is Nothing Then Return New PointF(-1, -1)
    Dim img = pb.Image
    Select Case pb.SizeMode
        Case PictureBoxSizeMode.StretchImage
            Return New PointF(clickPt.X * img.Width / pb.ClientSize.Width, clickPt.Y * img.Height / pb.ClientSize.Height)
        Case PictureBoxSizeMode.Zoom
            Dim ratio = Math.Min(pb.ClientSize.Width / img.Width, pb.ClientSize.Height / img.Height)
            Dim dispW = img.Width * ratio, dispH = img.Height * ratio
            Dim offX = (pb.ClientSize.Width - dispW) / 2, offY = (pb.ClientSize.Height - dispH) / 2
            If clickPt.X < offX OrElse clickPt.X > offX + dispW Then Return New PointF(-1, -1)
            Return New PointF((clickPt.X - offX) / ratio, (clickPt.Y - offY) / ratio)
        Case Else
            ' Normal/Center/AutoSize: treat as 1:1 with possible centering
            Dim offX = (pb.ClientSize.Width - img.Width) / 2, offY = (pb.ClientSize.Height - img.Height) / 2
            Return New PointF(clickPt.X - offX, clickPt.Y - offY)
    End Select
End Function

When storing: convert image pixel coords to normalized values (x/img.Width, y/img.Height) and write those to the database. When redrawing, reverse the transform to get the display position and draw markers in the PictureBox.Paint event (so markers stay correct after moves/resizes). Additional tips: avoid magic offsets, use the MouseEventArgs.Location when handling PictureBox.MouseClick, handle clicks outside the displayed image (letterbox) and account for DPI/scaling if the app runs on high-DPI displays. This combination ensures coordinates remain correct regardless of form position or screen resolution.

Recommended Answers

All 2 Replies

why dont you use scaling??

like scaling 1024 to 1600

Member Avatar for Member #857553

Use PointToClient.

If you want the coordinates relative to the form Use:

Dim clientPoint as point = Me.PointToClient(Cursor.Position)

If you want the coordinates relative to the picBox Use:

Dim clientPoint as point = PictureBox1.PointToClient(Cursor.Position)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.