0

I have an InkCanvas with strokes present. I wish to use only the strokes, or part of the strokes, that fall within a given region. In short, I wish to clip any ink outside of this region. I can't figure out how to cast this correctly:

            Rect r = new Rect(100,100,100,100);

            StrokeCollection x = InkCanvas.Strokes
                .Select(s => s.GetClipResult(r));
pushpraj
  • 13,458
  • 3
  • 33
  • 50
Alan Wayne
  • 5,122
  • 10
  • 52
  • 95
  • What is wrong with what you have now? According to documentation that is what you would need – nakiya Jun 02 '14 at 04:51
  • Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Windows.Ink.StrokeCollection'. An explicit conversion exists (are you missing a cast?) – Alan Wayne Jun 02 '14 at 05:01

1 Answers1

2

LINQ method Select<T>() returns an IEnumerable<T> and you are attempting to assign it to x which is not IEnumerable<T> type. so the correct syntax would be

IEnumerable<StrokeCollection> x = InkCanvas.Strokes.Select(s => s.GetClipResult(r));

and if you wish to have to have first collection then x.First() or x.FirstOrDefault() will return the first StrokeCollection from the IEnumerable<StrokeCollection> where former will throw exception if it is empty and latter will return null which is default for reference type StrokeCollection.

Retrieve all strokes in a new stroke collection

the LINQ can be modified to

StrokeCollection strokes = new StrokeCollection(InkCanvas.Strokes.SelectMany(s => s.GetClipResult(r)));

this will retrieve all the strokes from the clip region and create a new StrokeCollection with them.

pushpraj
  • 13,458
  • 3
  • 33
  • 50
  • Thank you. That does answer my question. But what I would like is linq code that does this: StrokeCollection strokes = new StrokeCollection(); foreach (Stroke s in InkCanvas.Strokes) { StrokeCollection rs = s.GetClipResult(r); strokes.Add(rs); } – Alan Wayne Jun 02 '14 at 05:19
  • 1
    I updated my answer for your requirement so you can use a single LINQ hence you need not to do a for each and add them individually. – pushpraj Jun 02 '14 at 05:41
  • I am glad it work for you. You may also choose to vote up the answer to keep it on the top. – pushpraj Jun 02 '14 at 06:44