tags:

views:

424

answers:

3

Hi, this is a piece of XAML script that draw a Bezier code. What I need is a pure C# code behind that achieve the same result but not using XAML.

Can anybody help convert it into C# code?

Thanks in advance!

Mike

<Path Stroke="Black" StrokeThickness="1">
  <Path.Data>
    <PathGeometry>
      <PathGeometry.Figures>
        <PathFigureCollection>
          <PathFigure StartPoint="10,100">
            <PathFigure.Segments>
              <PathSegmentCollection>
                <BezierSegment Point1="100,0" Point2="200,200" Point3="300,100" />
              </PathSegmentCollection>
            </PathFigure.Segments>
          </PathFigure>
        </PathFigureCollection>
      </PathGeometry.Figures>
    </PathGeometry>
  </Path.Data>
</Path>
+2  A: 

I tested this code and it works.

        Path path = new Path();
        path.Stroke = new SolidColorBrush(Colors.Black);
        path.StrokeThickness = 10;
        PathGeometry pg = new PathGeometry();
        PathFigureCollection pfc = new PathFigureCollection();
        PathFigure fig = new PathFigure();
        PathSegmentCollection psc = new PathSegmentCollection();
        BezierSegment bs1 = new BezierSegment(new Point(100, 0), new Point(200, 200), new Point(300, 100), true);
        psc.Add(bs1);
        fig.Segments = psc;
        pfc.Add(fig);
        pg.Figures = pfc;
        path.Data = pg;
John
A: 

I used John's code inside window loaded() but the curve was not there. while the xaml code was perfectly working. It neither shows any error nor the desired output. Or is it" I have placed 2nd code in wrong place. I had placed the whole c# code in wpf class.

Seeking suggestions, Thanks in advance

Anahcra
A: 

Path path = new Path(); path.Stroke = Brushes.Black; path.StrokeThickness = 1; PathGeometry pg = new PathGeometry(); PathFigureCollection pfc = new PathFigureCollection(); PathFigure fig = new PathFigure(); fig.StartPoint = new Point(10,100); PathSegmentCollection psc = new PathSegmentCollection(); BezierSegment bs1 = new BezierSegment(new Point(100, 0), new Point(200, 200), new Point(300, 100), true); psc.Add(bs1); fig.Segments = psc; pfc.Add(fig); pg.Figures = pfc; path.Data = pg; canvas.Children.Add(path);

subho100
John was perfect but missed two lines. fig.StartPoint = new Point(10,100);andcanvas.Children.Add(path);
subho100