移动 UWP InkStrokes 以进行离屏渲染

我正在捕获InkStrokes并且需要在背景中创建一个缩放的笔画位图图像。无论墨水的边界框有多大,捕获的图像都需要具有统一的大小。


例如,如果绘制原始墨水笔划,并且墨水画布上的边界框顶部/左侧为 100,100 且大小为 200,200,我希望墨水从新渲染位图的 0,0 处开始,即 50,50 大小(忽略现在笔画宽度的影响)。


我已经弄清楚如何缩放墨迹笔划(感谢StackOverflow),但不知道如何移动笔划。现在,似乎我必须创建一个InkCanvas大小的位图,渲染缩放后的墨水,然后将更大的图像裁剪为正确的大小。


我试过使用InkStroke.PointTranslate通过


var scaleMatrix = Matrix3x2.CreateScale(scale);

scaleMatrix.Translation = -offset; // top/left of ink stroke bounding box

stroke.PointTransform = scaleMatrix;

但坐标不正确。


非常感谢任何帮助。


收到一只叮咚
浏览 280回答 2
2回答

慕的地8271018

您可以通过矩阵相乘来组合变换。这对我有用var strokes = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();var boundingBox = inkCanvas.InkPresenter.StrokeContainer.BoundingRect;var matrix1 = Matrix3x2.CreateTranslation((float)-boundingBox.X, (float)-boundingBox.Y);var matrix2 = Matrix3x2.CreateScale(0.5f);var builder = new InkStrokeBuilder();var newStrokeList = new List<InkStroke>();foreach (var stroke in strokes){&nbsp; &nbsp; newStrokeList.Add(builder.CreateStrokeFromInkPoints&nbsp; &nbsp; &nbsp; &nbsp; (stroke.GetInkPoints(), matrix1 * matrix2));}//Add the translated and scaled strokes to the inkcanvasinkCanvas.InkPresenter.StrokeContainer.AddStrokes(newStrokeList);

慕标5832272

也许我仍然做错了什么,但看起来您不能将InkStrokeBuilder.CreateStrokeFromInkPoints与一种以上的转换一起使用。我尝试了各种组合/方法,但无法让它发挥作用。这是我的解决方案...&nbsp;private static IList<InkStroke> GetScaledAndTransformedStrokes(IList<InkStroke> strokeList, float scale)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var builder = new InkStrokeBuilder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newStrokeList = new List<InkStroke>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var boundingBox = strokeList.GetBoundingBox();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var singleStroke in strokeList)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var translateMatrix = new Matrix(1, 0, 0, 1, -boundingBox.X, -boundingBox.Y);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newInkPoints = new List<InkPoint>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var originalInkPoints = singleStroke.GetInkPoints();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var point in originalInkPoints)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newPosition = translateMatrix.Transform(point.Position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newInkPoint = new InkPoint(newPosition, point.Pressure, point.TiltX, point.TiltY, point.Timestamp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newInkPoints.Add(newInkPoint);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newStroke = builder.CreateStrokeFromInkPoints(newInkPoints, new Matrix3x2(scale, 0, 0, scale, 0, 0));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newStrokeList.Add(newStroke);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return newStrokeList;&nbsp; &nbsp; &nbsp; &nbsp; }我最终不得不应用我自己的翻译转换,然后使用builder.CreateStrokeFromInkPoints和一个应用了比例矩阵来获得我想要的结果。 GetBoundingBox是我自己的扩展:&nbsp;public static class RectExtensions&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public static Rect CombineWith(this Rect r, Rect rect)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var top = (r.Top < rect.Top) ? r.Top : rect.Top;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var left = (r.Left < rect.Left) ? r.Left : rect.Left;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var bottom = (r.Bottom < rect.Bottom) ? rect.Bottom : r.Bottom;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var right = (r.Right < rect.Right) ? rect.Right : r.Right;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newRect = new Rect(new Point(left, top), new Point(right, bottom));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return newRect;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP