2010-06-21 2 views
1

J'essaie de créer 1 forme composite complexe sur un InkCanvas, mais je dois faire quelque chose de mal, car ce que je m'attendais à ce qu'il ne se produise pas. J'ai essayé plusieurs incarnations différentes d'accomplir ceci.(Composite) Confusion de géométrie dans C#

J'ai donc cette méthode.

private void InkCanvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e) 
    { 
     Stroke stroke = e.Stroke; 

     // Close the "shape". 
     StylusPoint firstPoint = stroke.StylusPoints[0]; 
     stroke.StylusPoints.Add(new StylusPoint() { X = firstPoint.X, Y = firstPoint.Y }); 

     // Hide the drawn shape on the InkCanvas. 
     stroke.DrawingAttributes.Height = DrawingAttributes.MinHeight; 
     stroke.DrawingAttributes.Width = DrawingAttributes.MinWidth; 

     // Add to GeometryGroup. According to http://msdn.microsoft.com/en-us/library/system.windows.media.combinedgeometry.aspx 
     // a GeometryGroup should work better at Unions. 
     _revealShapes.Children.Add(stroke.GetGeometry()); 

     Path p = new Path(); 
     p.Stroke = Brushes.Green; 
     p.StrokeThickness = 1; 
     p.Fill = Brushes.Yellow; 
     p.Data = _revealShapes.GetOutlinedPathGeometry(); 

     selectionInkCanvas.Children.Clear();   
     selectionInkCanvas.Children.Add(p); 
    } 

Mais voici ce que je reçois: http://img72.imageshack.us/img72/1286/actual.png

Alors, où vais-je tort?

TIA, Ed

+1

Que voulez-vous atteindre?? –

+0

Ce qui, dans mon esprit se produirait est-ce: http://img685.imageshack.us/img685/6761/expected.png –

Répondre

2

Le problème est que la géométrie retournée par stroke.GetGeometry() est un chemin autour de la course, de sorte que la zone vous remplissez jaune est juste au milieu de la course. Vous pouvez voir plus clairement si vous faites les lignes plus épaisses:

_revealShapes.Children.Add(stroke.GetGeometry(new DrawingAttributes() { Width = 10, Height = 10 })); 

Vous pouvez faire ce que vous voulez si vous convertissez la liste des points de stylet à un StreamGeometry vous:

var geometry = new StreamGeometry(); 
using (var geometryContext = geometry.Open()) 
{ 
    var lastPoint = stroke.StylusPoints.Last(); 
    geometryContext.BeginFigure(new Point(lastPoint.X, lastPoint.Y), true, true); 
    foreach (var point in stroke.StylusPoints) 
    { 
     geometryContext.LineTo(new Point(point.X, point.Y), true, true); 
    } 
} 
geometry.Freeze(); 
_revealShapes.Children.Add(geometry); 
+0

Ok merci! C'est l'AHA! moment juste là .. la géométrie de la course est le chemin autour de la course. Tout a un sens maintenant, et fonctionne aussi! Merci encore! –