You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C# WPF的Polyline类中设置GeometryTransform?

解决方案:通过Path实现仅缩放顶点且不改变线宽

要实现仅缩放图形顶点、同时保持线条粗细不变的需求,最直接的方案是用Path替代Polyline,借助Geometry.Transform属性完成变换——这个变换仅作用于几何图形的坐标点,不会影响描边宽度。

步骤1:将Polyline替换为Path

把原Polyline的顶点转换为Path的几何结构,示例如下:
原Polyline代码:

<Polyline x:Name="MyPolyline" Points="10,10 50,50 100,30" Stroke="Black" StrokeThickness="2"/>

替换为Path:

<Path x:Name="MyPath" Stroke="Black" StrokeThickness="2">
    <Path.Data>
        <PathGeometry>
            <PathFigure StartPoint="10,10">
                <PolyLineSegment Points="50,50 100,30"/>
            </PathFigure>
        </PathGeometry>
    </Path.Data>
</Path>

步骤2:动态设置几何变换

在代码中直接修改Path的Geometry变换,即可实现顶点缩放,且线宽不受影响:

// 不等比缩放示例:X轴放大2倍,Y轴放大1.5倍
var pathGeometry = (PathGeometry)MyPath.Data;
pathGeometry.Transform = new ScaleTransform(2.0, 1.5);

如果需要更复杂的矩阵变换,也可以使用MatrixTransform:

var transformMatrix = new Matrix(2.0, 0, 0, 1.5, 0, 0);
pathGeometry.Transform = new MatrixTransform(transformMatrix);

方案优势

  • 无需重新生成或绑定所有顶点,直接修改Transform即可,性能开销低
  • Geometry.Transform属于几何层级的变换,仅作用于坐标点,完全不会影响StrokeThickness的实际显示尺寸
  • 支持任意比例的缩放(包括不等比缩放),解决了RenderTransform/LayerTransform会缩放线宽、以及等比缩放时反向调整线宽不适用的问题

若坚持使用Polyline的局限性

如果一定要保留Polyline,常规方案(如Viewbox嵌套)会同时缩放线宽,且不等比缩放时无法通过调整StrokeThickness来抵消——因为线宽是单一数值,无法分别对应X/Y轴的缩放比例,因此Path方案是最优解。

内容的提问来源于stack exchange,提问作者András

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 05:25:00