Cyotek Forums

Source Code => Source Code => Topic started by: ruland on September 08, 2017, 04:09:42 PM

Title: How to get image pixel position, regardless of pan and zoom?
Post by: ruland on September 08, 2017, 04:09:42 PM
I just stumbled upon this awesome control, and I'm still learning how to use it. So, please forgive me if my question has been asked before or the answer appears obvious.

I am looking for an easy way to get the absolute image coordinates (in pixels) of a point the mouse cursor is pointing at. (I found the method "GetOffsetPoint()", which seems to do the exact opposite(?))

As the user moves the mouse across the image box or clicks a point on the image, I want my application to display the image pixel coordinates in a status bar or in a tooltip box.

Thanks.

EDIT: I am sorry - just found the method: PointToImage(). :-)
Title: Re: How to get image pixel position, regardless of pan and zoom?
Post by: Richard Moss on September 09, 2017, 10:18:25 AM
Hello,

Welcome to the forums and thanks for the question! Glad you found the answer.

My only addition is by default this will return Point.Empty if the specified point is outside the bounds of the image,  however of course this is a perfectly valid point if the user happens to hover their mouse over the first pixel in the image. So you may wish to check if the point is in the image region first via IsPointInImage. The code below is extracted from the demonstration program and shows this in action.

private void UpdateCursorPosition(Point location)
{
  if (!fitCursorLocationToBoundsToolStripMenuItem.Checked || imageBox.IsPointInImage(location))
  {
    Point point;

    point = imageBox.PointToImage(location);

    cursorToolStripStatusLabel.Text = this.FormatPoint(point);
  }
  else
  {
    cursorToolStripStatusLabel.Text = string.Empty;
  }
}


Glad you like the control, please let me know if you encounter any issues or have suggestions.

Regards;
Richard Moss
Title: Re: How to get image pixel position, regardless of pan and zoom?
Post by: ruland on September 09, 2017, 10:53:21 AM
Thanks a lot. I really like your control!