user2551669
user2551669

Reputation:

How Can I Convert a shape like rectangle or circle to stylus point collection in WPF?

My question has three parts:
I Can draw shapes like line, Circle, Rectangle and .. on WPF Canvas. I want to use InkCanvas features like erasing and moving strokes.

  1. Is it possible to convert this shape to a collection of stylus points and add this collection to InkCanvas?
  2. If it's possible how can I do that?
  3. Is there a better approach to this situation?

Guide me please.

Upvotes: 0

Views: 1192

Answers (1)

Khosrow
Khosrow

Reputation: 69

first of all, the answer is Yes. you can convert paths to stroke collection and then add them to the InkCanvas.
For the second part of your question, the answer should be something like this:

Point mypoint;
Point tg;
var pointCollection = new List<Point>();
    for (var i = 0; i < 500; i++)
        {
           SomePath.Data.GetFlattenedGeometryPath()
                         .GetPointAtFractionLength(i / 500f, out mypoint, out tg);
            pointCollection.Add(p);
        }

For stylus point and stylus point collection:

 StylusPointCollection StPoints = new StylusPointCollection();

add stylus points during converting path to collection of points by:

 StPoints.Add(new StylusPoint(p.X, P.Y));

And after this step calling Stroke method to create a collection of strokes from your stylus collection:

Stroke st = null;
st = new Stroke(StPoints);

Update
Yes! there is a better ways for adding shapes to inkCanvas.

  1. You can define this stylus points shape directly and add them using MouseDown, MouseMove.. for example for drawing a Rectangle:

    pts.Add(new StylusPoint(mouseLeftDownPoint.X, mouseLeftDownPoint.Y));
    pts.Add(new StylusPoint(mouseLeftDownPoint.X, currentPoint.Y));
    pts.Add(new StylusPoint(currentPoint.X, currentPoint.Y));
    pts.Add(new StylusPoint(currentPoint.X, mouseLeftDownPoint.Y));
    pts.Add(new StylusPoint(mouseLeftDownPoint.X, mouseLeftDownPoint.Y));
    
  2. Or Override DrawCore method of Stroke Class and define a new stroke type. Custom Rendering Ink (MSDN)

Upvotes: 2

Related Questions