Tutorial :Draw Sine Wave in WPF


This one seems quite stupid, but I'm struglling from an hour to do this,

How to Draw the Sine Wave in WPF??



Draw lines between points which you calculate with Math.Sin function. You'll have to decide how many points per cycle to use, a compromise between drawing speed and accuracy. Presumably you'll also need to scale the amplitude to suit the area on the screen, since Sin function will return a value between +1 and -1.


How are you doing your "Drawing". WPF doesn't have OnPaint events like Winforms so that might prove a little tricky. The way to do this in WinForms would have been to use the Graphics.DrawBezier method

e.Graphics.DrawBezier(new Pen(new SolidBrush(Color.Red)),                             new Point(0, 100),                             new Point(50, 0),                             new Point(50, 200),                             new Point(100, 100));  

Maybe that's helpful, but I'm not even sure how to draw directly to the WPF Canvas.

A quick glance as MSDN shows that it has a BezierSegment control that maybe of use to you.


If want curves between your points you can use a PolyBezier to draw your Sine Wave with a PointCollection calculated from the Math.Sin method. Alternitavely you could create a lot of BezierSegments which flow on from each other. Finally just add your PolyBezier or BezierSegments to your form with drawingarea.Children.Add(curve) where drawing area is the grid or canvas you're drawing to.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »