Pregunta

Estoy viendo el bicho más raro con el siguiente código.

Tengo un PathGeometry a la que añadió un PathFigure para que yo pueda añadir LineSegments a ella.

Esto es lo que hago:

_pathGeometry.Figures.Add(_pathFigure);
_pathFigure.StartPoint = new Point(4, 0);
LineSegment lineSegment1 = new LineSegment(new Point(4, -10), true);
LineSegment lineSegment2 = new LineSegment(new Point(4, 0), true);
_pathFigure.Segments.Add(lineSegment1);
_pathFigure.Segments.Add(lineSegment2);

a continuación, lo dibujo:

using (DrawingContext drawingContext = RenderOpen())
   drawingContext.DrawGeometry(null, _pen, _pathGeometry);

Lo que debería ver:

WPF debe trazar una línea vertical que va de 0 a -10 y de nuevo a 0. La última parte (de nuevo a 0) no se ve porque está dibujado en la misma x píxeles. Pero la última parte hace lo siguiente:

Lo que veo:

WPF dibuja una línea que va de 0 a -15ºC. No tiene sentido para mí. Esta diferencia 5 píxeles sucede cada vez que dibuje una línea vertical en la parte superior de la otra línea vertical como en el ejemplo anterior.

Por favor, que alguien me diga he cometido un error y esto no es un error de WPF.

¿Fue útil?

Solución

Creo que el problema tiene que ver con la forma en WPF hace "esquinas" en su trayectoria. A medida que el ángulo entre dos segmentos de línea se hace más aguda, la prestación de esquina se hace más evidente.

En su caso, usted tiene un (un segmento de línea de plegado sobre sí mismo) ángulo de cero grados, que es el caso más problemático.

No todo está perdido - hay varias soluciones posibles:

  • Dividir la PathFigure en dos PathFigures (que es su solución). Al hacer esto, se quita la esquina, y por lo tanto el problema.
  • Establecer propiedad StrokeLineJoin del Camino a bisel en lugar del Mitre (por defecto). Esto bisel apariencia esquina.
  • Bajar StrokeMiterLimit de Sendero. Esto hará que la esquina inferior "puntiagudo", como dicen los científicos.
  • Dile al segmento para eliminar de forma explícita que es "esquina-dad". Usted puede hacer esto mediante el establecimiento de la propiedad IsSmoothJoin true.

Para obtener más información sobre StrokeLineJoin, ver aquí . Para un interesante post acerca de cómo WPF hace esquinas biseladas, consulte aquí .

Otros consejos

Si se trata de algo como GraphicsPath, tendrá que establecer el punto final también.

Bueno, he encontrado una solución a este problema por mí mismo. Parece ser un error WPF .

Es necesario añadir las LineSegments a PathFigures individuales para no ver este extraño comportamiento:

PathFigure pathFigure1 = new PathFigure();
pathFigure1.StartPoint = new Point(4, 0);
LineSegment lineSegment1 = new LineSegment(new Point(4, -10), true);
pathFigure1.Segments.Add(lineSegment1);

PathFigure pathFigure2 = new PathFigure();
pathFigure2.StartPoint = new Point(4, -10);
LineSegment lineSegment2 = new LineSegment(new Point(4, 0), true);
pathFigure2.Segments.Add(lineSegment2);

pathGeometry.Figures.Add(pathFigure1);
pathGeometry.Figures.Add(pathFigure2);

todavía estaría agradecido si alguien podría explicar por qué veo este error si no lo hago de esta manera o si alguien puede confirmar que esto es un error.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top