WPF DrawGeometry does not draw stroke

て烟熏妆下的殇ゞ 提交于 2019-12-12 04:41:53

问题


I hope this question was not asked before, I searched everywhere.

My problem is that I'm drawing a set of coordinates on my wpf usercontrol with points, I managed to fill my polygon with background color but not with a stroke. Stoke for some reason is not drawing?

Here is my code on the OnRender event using the DrawingContext

System.Windows.Media.Pen penDrawing = new System.Windows.Media.Pen(System.Windows.Media.Brushes.OrangeRed, 2);

                            drawingContext.DrawGeometry(System.Windows.Media.Brushes.LightSeaGreen, penDrawing, streamGeometry);

Code in detail

StreamGeometry streamGeometry = new StreamGeometry();

System.Windows.Point firstCoordinate = new System.Windows.Point();

System.Windows.Point lastCoordinateAdded = new System.Windows.Point();

bool isMainPolygon = true;

using (StreamGeometryContext geometryContext = streamGeometry.Open())
{
    PointCollection points = new PointCollection();

    firstCoordinate = new System.Windows.Point(coordinatePoints.ProjectedCoordinates[0].X, coordinatePoints.ProjectedCoordinates[0].Y);

    geometryContext.BeginFigure(firstCoordinate, true, true);

    System.Windows.Media.Pen penDrawing = new System.Windows.Media.Pen(System.Windows.Media.Brushes.OrangeRed, 5);

    penDrawing.EndLineCap = PenLineCap.Round;

    penDrawing.DashCap = PenLineCap.Round;

    penDrawing.LineJoin = PenLineJoin.Round;

    penDrawing.StartLineCap = PenLineCap.Round;

    penDrawing.MiterLimit = 10.0;

    for (int i = 1; i < coordinatePoints.ProjectedCoordinates.Count; i++)
    {
        lastCoordinateAdded = new System.Windows.Point() { X = coordinatePoints.ProjectedCoordinates[i].X, Y = coordinatePoints.ProjectedCoordinates[i].Y };

        points.Add(lastCoordinateAdded);

        //////Check to see if Polygon is done drawing
        if (firstCoordinate == lastCoordinateAdded)
        {
            geometryContext.PolyLineTo(points, true, true);

            //drawingContext.DrawGeometry(isMainPolygon ? System.Windows.Media.Brushes.Green : System.Windows.Media.Brushes.White, pen, streamGeometry);

            streamGeometry.Freeze();

            drawingContext.DrawGeometry(null, penDrawing, streamGeometry);

            points = new PointCollection();

            streamGeometry = new StreamGeometry();

            if (i + 1 < coordinatePoints.ProjectedCoordinates.Count)
            {
                i++;

                isMainPolygon = false;

                firstCoordinate = new System.Windows.Point(coordinatePoints.ProjectedCoordinates[i].X, coordinatePoints.ProjectedCoordinates[i].Y);

                geometryContext.BeginFigure(firstCoordinate, true, true);
            }
        }
    }

    geometryContext.PolyLineTo(points, true, true);
}

coordinatePoints.State = Enums.CoordinateEnum.None;

}

I will glad to provide more details.

Thanks a million.


回答1:


As with everything, WPF scales the width of lines, so if the stroke width is too narrow relative to the extent of the geometry, the stroke can become invisible.




回答2:


When you create you geometry, make sure you set the stroked parameter on the StreamGeometryContext.LineTo method.

// Draw a line to the next specified point.
ctx.LineTo(new Point(100, 100), true /* is stroked */, false /* is smooth join */);
//                              ↑
//                              This parameter needs to be true.


来源:https://stackoverflow.com/questions/21756377/wpf-drawgeometry-does-not-draw-stroke

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!