Search code examples
c#wpfinkcanvasviewport3d

WPF InkCanvas access all pixels under the strokes


It seems that WPF's InkCanvas is only able to provide the points of the stroke (independent of the width and height of the stroke). For an application, I need to know all the points that are drawn by the InkCanvas.

For instance, assume that the width and height of the stroke are 16. Using this stroke size I paint a dot on the InkCanvas. Is there a straightforward way to obtain all 256 pixels in this dot (and not the center point of this giant dot alone)?

Why I care:
In my application, the user uses an InkCanvas to draw on top of a Viewport3D which is displaying a few 3D objects. I want to use all the points of the strokes to perform ray casting and determine which objects in the Viewport3D have been overlaid by the user's strokes.


Solution

  • I found a very dirty way of handling this. If anyone knows of a better method, I'll be more than happy to upvote and accept their response as an answer.
    Basically my method involves getting the Geometry of each stroke, traversing all the points inside the boundaries of that geometry and determining whether the point is inside the geometry or not.

    Here's the code that I am using now:

    foreach (var stroke in inkCanvas.Strokes)
    {
        List<Point> pointsInside = new List<Point>();
    
        Geometry sketchGeo = stroke.GetGeometry();
        Rect strokeBounds = sketchGeo.Bounds;
    
        for (int x = (int)strokeBounds.TopLeft.X; x < (int)strokeBounds.TopRight.X + 1; x++)
            for (int y = (int)strokeBounds.TopLeft.Y; y < (int)strokeBounds.BottomLeft.Y + 1; y++)
            {
                Point p = new Point(x, y);
    
                if (sketchGeo.FillContains(p))
                    pointsInside.Add(p);
            }
    }