How to get the position of a Coordinate with respe

2019-07-30 06:27发布

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?

enter image description here

1条回答
Explosion°爆炸
2楼-- · 2019-07-30 07:25

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

enter image description here

查看更多
登录 后发表回答