WPF DrawGeometry does not draw stroke
Asked Answered
S

2

0

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.

Sartorial answered 13/2, 2014 at 14:1 Comment(5)
It's the OrangeRed that you are not seeing? Why not make its size more than 2?Headline
@Headline i would like to see the code that generates the geometry. are the figure segments set to stroked?Edmonds
What is the extent of the geometry? I believe the stroke width might be scaled down so much to fit the extent that it becomes invisible.Attainment
It might be possible I am doing a scale transformation, but i did set the stroke size to something like 100. I did remove the fill just to null to see if I can see the stroke no luck though. Thanks for the help guysSartorial
@500 - Internal Server Error Thanks a million i increased the stoke size to 200 and when zooming in i could see the line. If you post as answer i will mark as correct one :)Sartorial
A
0

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.

Attainment answered 14/2, 2014 at 20:46 Comment(0)
E
1

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.
Edmonds answered 13/2, 2014 at 14:13 Comment(1)
I added the full length of code in the method, I did set the PolyLineTo property IsStroked to true.Sartorial
A
0

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.

Attainment answered 14/2, 2014 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.