How to determine an image's screen coordinates

Answered How to determine an image's screen coordinates

  • Friday, May 11, 2012 7:10 PM
     
     

    Hello!

    I have an image resembling a chessboard inside a picturebox displayed on the screen at runtime. I am using Visual Basic in Visual Studio 2008. I need to determine when a user clicks on a particular part of the image. I am able to do this without a problem when debugging on my computer, where I can experiment with trial-and-error clicking to get the screen coordinates of the portion of the image of interest. Everything changes, however, when the program is run on a screen with a different resolution and coordinates.

    I can easily get the coordinates for the picturebox that contains the image using PointToScreen. However, there seems to be no such property for the image within the picturebox, which is what I need.

    I would greatly appreciate any suggestions for determining the screen coordinates of an image within a picturebox, knowing that the exact coordiantes will differ slightly depending on the screen being used.

    Thank you very much,

    Rob

All Replies

  • Friday, May 11, 2012 7:35 PM
     
     

    You have to convert image coordinates to picturebox coordinates.  It depends upon the SizeMode property.  It's high school math.  You can also get it using reflection.  I posted the code in an earlier thread.


  • Friday, May 11, 2012 7:39 PM
     
     
    Thank you. I am using picturebox SizeMode property CenterScreen. Would you please share the specific formula to use?
  • Friday, May 11, 2012 8:33 PM
     
     
  • Saturday, May 12, 2012 1:48 PM
     
     
    Thanks for trying, but that post does not provide enough information. Anyone else?
  • Saturday, May 12, 2012 3:31 PM
     
     
    Thanks for trying, but that post does not provide enough information. Anyone else?

    What do you want to do besides locating a point in the image on the screen?
  • Saturday, May 12, 2012 5:52 PM
     
     

    In looking at the earlier post you referenced, it is not clear to me how to apply the code to my situation. This may well be a result of my ignorance. Nevertheless, I don't know what any of the properties represent.

    For sake of clarity, I have an image within a PictureBox. I need a way to find the screen coordinates of the image at runtime at varying screen sizes and resolutions. There is a particular region within the image that when clicked upon requires special handling. However, I cannot identify when this region is clicked upon if I do not know the screen coordinates of the image (or, as a result, the region of interest). I can find the region of interest if I know the image's coordinates since I know its size.

    In my application, there is a PictureBox called stimuluspicturebox. It is anchored on all four sides to a form, and has SizeMode = CenterImage. It is 918 x 668 in size. I am displaying an image file in it that is 792 x 612. At runtime, the image appears nicely centered on the screen. Using a temporary subroutine that displays e.Location.X and e.Location.Y in a MessageBox during the stimuluspicturebox.MouseClick event, I am able to find the screen coordinates of the region of interest within the image that needs special handling when clicked. This is, obviously, a crude method but works fine when I am only working on my screen because the values do not change. What I need is a method to detect this region of interest within the image when a future user is using a different sized screen or resolution. I understand that the coordinates of the PictureBox (stimuluspicturebox in my case) may be revealed by using PointToScreen:

    Dim aPoint As Point

    aPoint = stimuluspicturebox.PointToScreen(aPoint)

    MessageBox.Show(aPoint.ToString)

    This is useful for locating the coordinates of the stimuluspicturebox at varying screen sizes and resolutions, but leaves me without what I really need, which are the coordinates of the image within it.

    I would be grateful if someone would kindly enlighten me as to how to find the coordinates of the image.

    Sincerely,

    Rob

  • Saturday, May 12, 2012 6:36 PM
     
     Answered

    I guess you want the location on the screen in pysical dimension, not pixels.  That's much more difficult, but you still need the location in pixels.  Since CenterImage is linear, that is there is no scaling of the image,  Any point in the image is referenced to the picturebox by subtracting the location of the imagerectangle given in the link I posted.
    Try this code.  It gives the location of the mousedown event in picturebox, image and screen coordinates.  What additional do you need?

    Imports System.Reflection
    Public Class Form1
      
    Dim WithEvents PB As New PictureBox
      
    Dim Bmp As New Bitmap(792, 612)
      
    Dim BmpRect As Rectangle
      
    Protected Overrides Sub OnLoad(e As EventArgs)
        
    MyBase.OnLoad(e)
        PB.Size = 
    New Size(918, 668)
        PB.SizeMode = 
    PictureBoxSizeMode.CenterImage
        
    Using G As Graphics = Graphics.FromImage(Bmp)

          G.Clear(Color.Red)
        
    End Using
        PB.Image = Bmp
        
    Dim PI As PropertyInfo = PB.GetType.GetProperty("ImageRectangle"BindingFlags.GetProperty Or BindingFlags.NonPublic Or BindingFlags.Instance)
        BmpRect = 
    DirectCast(PI.GetValue(PB, Nothing), Rectangle)
        PB.Parent = 
    Me
        
    Me.ClientSize = PB.Size
      
    End Sub
      
    Private Sub PB_MouseDown(sender As Object, e As MouseEventArgsHandles PB.MouseDown
        
    Dim ImagePoint As New Point(e.X - BmpRect.X, e.Y - BmpRect.Y)
        
    Me.Text = "Mouse point = " + e.Location.ToString
        
    Me.Text += " Image point = " + ImagePoint.ToString
        
    Me.Text += " Screen point = " + PB.PointToScreen(e.Location).ToString
      
    End Sub
    End Class


  • Saturday, May 12, 2012 11:13 PM
     
     
    Thank you very much! I do not fully understand all of the code, but should be able to figure it out with your excellent example.