c#画布被过度绘制

本文关键字:绘制 | 更新日期: 2023-09-27 18:11:45

我的MainWindow中有一个Canvas,我在那里画一条线。当它绘制超过我的Canvas的宽度/高度时,在我的MainWindow中继续绘制。是我的代码出错了还是正常的?

<Canvas x:Name="coordinateSystem" HorizontalAlignment="Right" Height="580" Margin="0,10,283,0" VerticalAlignment="Top" Width="1024" Cursor="Cross" UseLayoutRounding="False"/>

这是我的函数,每次当我为我的行获得一个新的坐标时我调用:

// xOld, yOld and t are static
// t represents the time
private void drawPoly(double value) 
{    
    t++;
    Point pOne = new Point(xOld, yOld);
    Point pTwo = new Point(t, value);
    GeometryGroup lineGroup = new GeometryGroup();
    LineGeometry connectorGeometry = new LineGeometry();
    connectorGeometry.StartPoint = pOne;
    connectorGeometry.EndPoint = pTwo;
    lineGroup.Children.Add(connectorGeometry);
    System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
    path.Data = lineGroup;
    path.StrokeThickness = 1;
    path.Stroke = path.Fill = Brushes.Red;
    coordinateSystem.Children.Add(path);    
    xOld = t;
    yOld = value;
}

thx

PS:是否有办法保存所有绘制的点?我想稍后调整画布的大小(缩小/放大)或者如果时间要大移动我在画布上绘制的线,然后我需要再次绘制所有的点。

c#画布被过度绘制

画布不剪辑子元素。如果你想阻止子元素被绘制到画布之外,你需要将ClipToBounds设置为true或设置Canvas的Clip。