通过边界坐标获取像素坐标

发布于 2024-12-16 01:04:02 字数 306 浏览 5 评论 0原文

我正在开发一个项目,我需要找到所选区域的像素坐标。我只需单击 C# 图片框即可获得此坐标。我需要找到如图所示的灰色区域的像素坐标,以便更改该灰区域的颜色。 C# 中有定义的方法可以做到这一点吗?或者请了解如何存档此内容。

代码示例将不胜感激。

提前致谢。

选定坐标

选定坐标

所需区域

必填区域

I'm working on a project i need to find coordinates of pixels of selected area. I'm gaining this coordinates by simply clicking on a C# picture box. I need to find the pixel coordinates of the gray area as show in the picture in order to change the color of this ash area. is there a defined method in C# do do this? or please on how to archive this.

code samples will appreciated.

thanks in advance.

Selected Coordinates

Selected coordinates

Required Area

required area

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹊巢 2024-12-23 01:04:02

您需要的是多边形内的点算法( http://en.wikipedia.org/wiki/Point_in_polygon

static bool PointInPolygon(Point p, Point[] poly)
{
    Point p1, p2;
    bool inside = false;

    if (poly.Length < 3)
    {
        return inside;
    }

    Point oldPoint = new Point(poly[poly.Length - 1].X, poly[poly.Length - 1].Y);

    for (int i = 0; i < poly.Length; i++)
    {
        Point newPoint = new Point(poly[i].X, poly[i].Y);

        if (newPoint.X > oldPoint.X)
        {
            p1 = oldPoint;
            p2 = newPoint;
        }
        else
        {
            p1 = newPoint;
            p2 = oldPoint;
        }

        if ((newPoint.X < p.X) == (p.X <= oldPoint.X) 
            && ((long)p.Y - (long)p1.Y) * (long)(p2.X - p1.X) < ((long)p2.Y - (long)p1.Y) * (long)(p.X - p1.X))
        {
            inside = !inside;
        }

        oldPoint = newPoint;
    }

    return inside;
}

(来自 http://www.gamedev.net/topic/533455-point-in-polygon-c-implementation/ )

您也可以使用 .Net HitTestCore 方法(如果您使用) System.Windows.Shapes.Polygon 来表示您的多边形。但我不知道这有多容易。

What you need is a point-in-polygon algorithm ( http://en.wikipedia.org/wiki/Point_in_polygon )

static bool PointInPolygon(Point p, Point[] poly)
{
    Point p1, p2;
    bool inside = false;

    if (poly.Length < 3)
    {
        return inside;
    }

    Point oldPoint = new Point(poly[poly.Length - 1].X, poly[poly.Length - 1].Y);

    for (int i = 0; i < poly.Length; i++)
    {
        Point newPoint = new Point(poly[i].X, poly[i].Y);

        if (newPoint.X > oldPoint.X)
        {
            p1 = oldPoint;
            p2 = newPoint;
        }
        else
        {
            p1 = newPoint;
            p2 = oldPoint;
        }

        if ((newPoint.X < p.X) == (p.X <= oldPoint.X) 
            && ((long)p.Y - (long)p1.Y) * (long)(p2.X - p1.X) < ((long)p2.Y - (long)p1.Y) * (long)(p.X - p1.X))
        {
            inside = !inside;
        }

        oldPoint = newPoint;
    }

    return inside;
}

(from http://www.gamedev.net/topic/533455-point-in-polygon-c-implementation/ )

You may also use the .Net HitTestCore Method if you use System.Windows.Shapes.Polygon to represent your polygon. I can't tell how easy that will work though.

不如归去 2024-12-23 01:04:02

使用 Click 事件,并从事件中提取鼠标坐标。如果灰色区域是由函数定义的,您可以编写一个方法来检查它是否在指定的区域内。如果不是(这只是一个静态图像),您应该使用鼠标坐标来计算您单击了哪个像素,并检查其颜色值。可能有一种方法可以获取鼠标点击处的颜色值(但是,我可能会将该方法与OpenGL中的glReadPixel方法混淆)。

Use the Click event, and pull out the mouse coordinates from the event. If the gray area is defined by a function, you can write a method to check if it's within the area specified. If not (it's just a static image), you should use the mouse coordinates to calculate which pixel you have clicked, and check its color value. There might be a method to get the color value where the mouse clicks (however, I might be confusing the method with the glReadPixel method in OpenGL).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文