GDI+ Hittest on GraphicsPath line (stroke) only RRS feed

  • Question

  • Hi,

    I am using System.Drawing.Drawing2D and am basically visualising segmented unclosed GraphicsPath objects form array of points with .AddLines(), typically 50 points per line, thousands of lines. For hittesting I use .IsVisible().

    The hittest works, but unexpectedly. The problem is that it returns true whenever a hit is in the closed GraphicsPath, which is an area, whereas I only want the hittest on the path (stroke).

    My own solution is to create paths per segment in background and use this for hittesting, whilst drawing the whole path for visualisation. Has anybody a better and faster solution for this in GDI+ (I know that other technology e.g. D2D can help)?

    Thanks for your help,



    Code Snippets:

    using sd = System.Drawing;

            private sd.Drawing2D.GraphicsPath CreateTestGeometry(sd.Point p, int size) {
                sd.Point[] points = new sd.Point[4];
                points[0] = p;
                points[1] = new sd.Point(p.X, p.Y + size);
                points[2] = new sd.Point(p.X + size, p.Y + size);
                points[3] = new sd.Point(p.X + size, p.Y + size / 3);

                var path = new sd.Drawing2D.GraphicsPath();
                // path.CloseFigure(); // To illustrate the path is not closed.

                return path;

            private void RenderTest(sd.Graphics graphics, float width) {
                var myPen = new sd.Pen(sd.Color.Azure, width);
                foreach (var gp in _GraphicPaths) {
                    graphics.DrawPath(myPen, gp);

            public int HitTest(sd.Point p) {
                var cnt = 0;
                foreach (var gp in _GraphicPaths) {
                    if (gp.IsVisible(p))  {
                        var hg = GenerateHitGeometry(p, 3);
                        cnt += 1;
                if (cnt > 0) Draw(false);
                return cnt;  

    Friday, February 8, 2013 4:51 PM


All replies