Add polygon/ellipse to inkcanvas in uwp

邮差的信 提交于 2019-12-13 02:57:38

问题


I'm using Windows-universal-samples-master\Samples\InkAnalysis\InkAnalysis.sln of Microsoft's open source project. It can analysis the shape you draw, and convert to polygon or ellipse.

<Grid
<Canvas x:Name="canvas"/>
<InkCanvas x:Name="inkCanvas"/>
</Grid>

All the converted shapes are drawn to "canvas", not "inkCanvas", so they can not be saved as ink.

How to add the shapes to "inkCanvas"?


回答1:


We can use InkStrokeContainer.AddStroke to add to an InkStroke object to the collection managed by the InkStrokeContainer. If the shape is polygon, We can get the point from the InkAnalysisInkDrawing.Points and set them to the InkStrokeBuilder by the CreateStrokeFromInkPoints method.

For example:

private void AddPolygonToInkCanvas(InkAnalysisInkDrawing shape)
{
    var strokeBuilder = new InkStrokeBuilder();
    var strokes = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();
    strokeBuilder.SetDefaultDrawingAttributes(strokes[0].DrawingAttributes);
    System.Numerics.Matrix3x2 matr = strokes[0].PointTransform;
    List<InkPoint> inkPointslist = new List<InkPoint>();
    foreach (var item in shape.Points)
    {
        var intpoint = new InkPoint(new Point(item.X, item.Y), 0.5f);
        inkPointslist.Add(intpoint);
    }
    var lastintpoint = new InkPoint(new Point(shape.Points[0].X, shape.Points[0].Y), 0.5f);
    inkPointslist.Add(lastintpoint);
    IReadOnlyList<InkPoint> inkPoints = inkPointslist;
    InkStroke stroke = strokeBuilder.CreateStrokeFromInkPoints(inkPoints, matr);  
    inkCanvas.InkPresenter.StrokeContainer.AddStroke(stroke);
}

If the shape is ellipse, as far as I known we can not add it to the InkCanvas. We can not get the all of the ellipse, it only provides 4 points.



来源:https://stackoverflow.com/questions/46784818/add-polygon-ellipse-to-inkcanvas-in-uwp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!