Search code examples
c#wpfscaletransform

unable to scale the path's certain children in x axis only


My code is drawing combination of horizontal and vertical lines in wpf. I want to only scale horizontal lines.. this code is scaling both horizontal and vertical.

Path path = new Path();
            SolidColorBrush brush = new SolidColorBrush();
            brush.Color = Colors.Red;
            path.Stroke = brush;
            path.StrokeThickness = 4;
             LineGeometry lineGeometry = new LineGeometry();
            lineGeometry.StartPoint = new Point(10, 100);
            lineGeometry.EndPoint = new Point(100, 100);
            LineGeometry lineGeometry2 = new LineGeometry();
            lineGeometry2.StartPoint = new Point(100, 100);
            lineGeometry2.EndPoint = new Point(100, 200);

            LineGeometry lineGeometry3 = new LineGeometry();
            lineGeometry3.StartPoint = new Point(100, 200);
            lineGeometry3.EndPoint = new Point(200, 200);
            LineGeometry lineGeometry4 = new LineGeometry();
            lineGeometry4.StartPoint = new Point(200, 200);
            lineGeometry4.EndPoint = new Point(200, 100);
            LineGeometry lineGeometry5 = new LineGeometry();
            lineGeometry5.StartPoint = new Point(200, 100);
            lineGeometry5.EndPoint = new Point(300, 100);
            GeometryGroup geometryGroup = new GeometryGroup();
            geometryGroup.Children.Add(lineGeometry);
            geometryGroup.Children.Add(lineGeometry2);

            geometryGroup.Children.Add(lineGeometry3);

            geometryGroup.Children.Add(lineGeometry4);

            geometryGroup.Children.Add(lineGeometry5);

            path.Data = geometryGroup;


            var scaling = new ScaleTransform(2, 1);

            path.RenderTransform = scaling;
            canvas1.Children.Add(path);

Solution

  • If you only want to scale the geometry, but not the Path's StrokeThickness, do not set the Path's RenderTransform.

    Instead, directly transform the Geometry:

    geometryGroup.Transform = new ScaleTransform(2, 1);
    path.Data = geometryGroup;
    
    canvas1.Children.Add(path);