I have an application with a display control on a form, and I want to react to clicks inside the display by coloring the pixel that has been clicked by the user. But the mouse coordinate does not seem to correlate with the pixel that gets colored, especially when the display has been zoomed in
The conversion between mouse coordinates and the coordinates of a pixel in an image is not as trivial as it sounds.
First of all, many GUI toolkits give the mouse coordinates in screen coordinates and you’ll need to convert them to coordinates relative to the display control’l client area’s left/top corner. In C# you’d use
var clientCoordinate = axCVdisplay.PointToClient(System.Windows.Forms.Cursor.Position);
and with MFC or Windows SDK the code would look like this
POINT p;
GetCursorPos(&p);
ScreenToClient(m_cvDisp.m_hWnd, p);
Once client coordinates for the display control are available they’ll need to be converted to image coordinates using the display control’s member functions ClientToImage
(when working with the display DLL it would be DisplayClientToImage
instead to get the x/y coordinate of the pixel in the CVB image.
This conversion takes into account the image coordinate system, so if that is non-trivial (has been modified) you’ll need to call ImageToPixelCoordinates
from the CVCImg.dll to finally get the pixel coordinate. Note that the coordinate may be fractional, in which case you’ll need to properly round it.
When working with the display control’s MouseDown
event (or similar) please note that in that case the coordinates of the mouse click are already given in client coordinates, not on screen coordinates, and therefore the first conversion step may be omitted.