How to draw a Bezier curve programmatically in WPF? - wpf

I need to write a simple WPF program to draw a Bezier curve, but I have to draw it programmatically since I need to allow user to modify the shape interactively.
Any code sample to do this task is highly appreciated!
Thanks,
Mike

Have a look at the Path Markup Syntax to get a feel for the raw drawing primitives available to you in WPF.
You could use either cubic or quadratic Bezier curves (each has a smoothed version too) depending on how you want to define the control points.
As for rendering the control points on screen and allowing the user to drag them about you might like to look into adorners and possibly the Thumb class.

Related

How to replace DrawClosedCurve / FillClosedCurve when moving from WinForms (GDI+) to WPF?

I've got an array of points (X,Y) which constitute a convex hull (a simple, irregularly shaped contour). Rather than rendering a polygon with straight edges, I want to render them with an approximate "smoothly curved" contour that passes through all of these points.
In WinForms/GDI+, this could be accomplished with the Graphics.DrawClosedCurve or Graphics.FillClosedCurve methods. There does not appear to be an equivalent in WPF.
I've looked into drawing using things like Path and BezierSegment, but I'm not sure if (and how) these can be used to generate a continuous closed curve, given a set of points. It appears that to do this, I'd perhaps have to generate a set of control points based on my contour as an intermediate step?
I have tried using the GDI+ methods to render onto a System.Drawing.Bitmap and then displaying that in the WPF application. This works, but the performance (particularly the conversion from System.Drawing.Bitmap to BitmapSource) is poor and not sufficient for the application. This is why I'm looking for a pure WPF solution.
Has anyone been able to draw a closed curve based off a set of points in WPF?
Unfortunately, there is no single-method equivalent to DrawClosedCurve in WPF, even though it's been requested. So you are left with at least two options:
Host a native window within your WPF window and perform all your drawing on it.
Implement your own cardinal spline drawing. You are on the right track with Bezier segments. However, there are existing implementations of it you can look at out there.

Irregular Polygon

Take a look at the attached pic, I need to mimic the grey irregular polygon header at the top of the form. If it wasn't for the angled portion on the right-hand side, it would be straightforward. What is the best method to create it? Can this be created in XAML? Should I use an image? Any pointers are greatly appreciated.
This can easily be done via the standard drawing capabilities in XAML. See Shapes and Basic Drawing for details on making custom shapes.

How to render a VisualElement to a Vector Based Image?

I am working on a Chart Control,
I need to implement the Chart Snapshot feature for capturing a vector based image in high quality of Curves and Texts, in any requested size.
Can anyone suggest me any solution or a pointer to resolve this problem.
Any kind of help in deeply appreciated.
If you have to save your image in a vector format you can using tracing. Potrace is an open source bitmap-to-vector tracer library (but considers that bitmap tracing is imperfect). Also considers that wpf is linked closely with XAML, a vector graphics markup languages, so you may want convert raster graphics to XAML (though Charles Petzold you can't embed a bitmap in a XAML file)
If you need to draw a vector image you can use the Shape element that provides a base class for shape elements, such as Ellipse, Polygon, and Rectangle; and add the shape as Panel child. If you are dealing with thousands of shape I suggest to use the DrawingVisual class, a visual object that can be used to render vector graphics on the screen, and its RenderOpen method.
To zoom you have to work with transformations, in particular ScaleTrasnform and apply the transformation to your panel or to your shapes.
Hope this help.
You can take a look at XamlToy but I have not already try it.
http://xamltoys.codeplex.com/

How to build the following visual

I am not a real designer and I would like to know if anyone have an idea how can I build the following visual :
The idea is to get 2 rings where inside I will draw secific number of pie represented as "Interactive shape or button". Base on the number of shape, they should cover the whole circle.
In addition to that, I need to be able to interact with each pie shape, and inside and oustide edge of those shape should be a perfect arc based on the circle diameter.
As I am not a perfect designer, how can I represent this visual ?
I was thinking of using a custom panel but then how to draw each panel shape in order that they gets perfect inside and ouside arc and offer interactivity with each of them ?
Thanks for your help
I would appreciate samples as well
Theres actually a Silverlight tutorial on making something exactly like this.
If you didnt want to use that, you could always do it in javascript. Here's an example using Rapheal js
I would recommend looking into WPF Geometry and how you can create custom controls (such as the Circular Gauge Custom Control over on CodeProject) using said Geometry.
I've never created a control quite like what your suggesting, but I would image you would define some form of area that can contain children and style it so that it forms the circular shape you want. Then, adding interactive regions should be as simple as adding controls to standard containers.
Here's a link to a "generic radial panel that can be used to host any items" which subclasses Panel.

Interactive 3D object in Surface

How to create a interactive 3D object in WPF? For example it can be a 3D Cube which can be rotated and with "tap" gesture for each side triggering different action.
Take a look at the ViewPort3D class - it has a camera that you can rotate in 3d. Just hook up a MouseDown event handler and modify the camera position.
http://www.kindohm.com/technical/wpf3dtutorial.htm
Interactive 3D effects are certainly doable in WPF/Surface, although it'll take a bit of work. For example, have a look at the stuff here, especially the rolling globe about 1:50 into the first picture. The implementation shown is Surface, but that is largely WPF with some different input mechanisms... Unfortunately, he doesn't show the code...
Take a look at codeplex.com/3DTools. Wrap your ViewPort3D into Interactive3DDecorator.

Resources