How to get image pixel position, regardless of pan and zoom?

Started by ruland, September 08, 2017, 04:09:42 PM

Previous topic - Next topic

ruland

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(). :-)

Richard Moss

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
Read "Before You Post" before posting (https://forums.cyotek.com/cyotek-webcopy/before-you-post/). Do not send me private messages. Do not expect instant replies.

All responses are hand crafted. No AI involved. Possibly no I either.

ruland