Search code examples
c#.netdrawingsystem.drawing

How to know if a GraphicsPath contains a point in C#


I'm using .NET to draw a diagram, and I want to highlight objects when the user performs a click on them. It is easy when a figure is fully contained in a rectangle:

if (figure.Bounds.Contains(p)) // bounds is a rectangle

But I don't know how to manage it if the figure is a complex GraphicsPath.

I have defined the following GraphicsPath for the figure (the green circle).

GraphicsPath

I want to highlight the figure when the user click on it. I would like to know if a Point is contained in that GraphicsPath.

Any ideas? Thanks in advance.


Solution

  • I don't know a DrawingPath (you mean probably; graphics.DrawPath) but a GraphicsPath has the IsVisible method to check if a point is in the path.

    bool isInPath = graphicsObj.IsVisible(point)