How to get the position of a Coordinate with respe

2019-07-30 06:50发布

问题:

I am on Wpf and I have a list of coordinates where I draw them on a Bitmap Image. My Bitmap file is 1000 * 1000 and it gets filled in a 680 * 440 Image control. Now what I am trying to accomplish is to highlight the coordinates that are near the mouse cursor, when mouse is hoovering my Image.

on MouseMove() event handler, I call this function and pass to it my mouse position with respect to the Image control:

public void HighLightNearbyDots(Point MousePosition)
{
    int Distance;
    CoordPoint temp = new CoordPoint();
    temp.X = MousePosition.X;
    temp.Y = MousePosition.Y;

    foreach (var point in myDisplayedCoords)
    {
        Distance = (int)(temp - point); // using subtraction operator that I wrote

        if (Distance < 10)
        {
            point.Color = Colors.Blue;
        }
        else
        {
            point.Color = InitialCoordColor; // Aqua
        }
    }

    DrawImage();
}

Yes I redraw my image on every call to reflect the changes. Maybe the issue is that I need to scale or calculate some ratio between the 1000 * 1000 file size and the 680 * 440 control size to hit the exact pixel.. But I am not sure what is the issue. Below is the current result which is killing me since the morning. Could any one help me approach that?

回答1:

Based on this How to scale a coordinate system? Now we know the equation. Then I use it this way:

int Distance;
CoordPoint temp = new CoordPoint();
temp.X = MousePosition.X / 660 * Bitmap.Width;
temp.Y = Bitmap.Height - (MousePosition.Y / 440 * Bitmap.Height); // y is flipped