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.

Ockert
  • 3
  • 4
  • It's the OrangeRed that you are not seeing? Why not make its size more than 2? – Rui Feb 13 '14 at 14:04
  • @Rui i would like to see the code that generates the geometry. are the figure segments set to stroked? – Gusdor Feb 13 '14 at 14:09
  • 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. – 500 - Internal Server Error Feb 13 '14 at 14:41
  • 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 guys – Ockert Feb 14 '14 at 06:21
  • @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 :) – Ockert Feb 14 '14 at 06:33

2 Answers2

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.
Gusdor
  • 14,001
  • 2
  • 52
  • 64
  • I added the full length of code in the method, I did set the PolyLineTo property IsStroked to true. – Ockert Feb 14 '14 at 06:24
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.