confusione (Composite) Geometry in C #
Domanda
Sto cercando di creare 1 forma composita complessa su un InkCanvas, ma devo essere facendo qualcosa di sbagliato, come quello che mi aspettavo per accadere, non lo è. Ho provato diverse incarnazioni differenti di realizzare questo.
Così ho questo metodo.
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);
}
Ma questo è ciò che ottengo: http://img72.imageshack.us/img72/1286/actual.png
Allora, dove sto andando male?
TIA, Ed
Soluzione
Il problema è che la geometria restituito da stroke.GetGeometry () è un percorso intorno alla corsa, quindi l'area si sta riempiendo con il colore giallo è solo la metà della corsa. Si può vedere questo più chiaramente se si fanno le linee più spesse:
_revealShapes.Children.Add(stroke.GetGeometry(new DrawingAttributes() { Width = 10, Height = 10 }));
Si può fare quello che vuoi se si converte l'elenco dei punti stilo per uno StreamGeometry te stesso:
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);