WPF DrawGeometry不绘制笔画

时间:2014-02-13 14:01:07

标签: c# wpf polygon stroke drawingcontext

我希望以前没有问过这个问题,我到处搜索。

我的问题是我在我的wpf usercontrol上用点绘制了一组坐标,我设法用背景颜色填充我的多边形但不用笔划填充。斯托克出于某种原因没有画画?

以下是使用DrawingContext

的OnRender事件的代码
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);

详细代码

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;

}

我很乐意提供更多细节。

万分感谢。

2 个答案:

答案 0 :(得分:0)

创建几何图形时,请确保在stroked方法上设置StreamGeometryContext.LineTo参数。

// 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.

答案 1 :(得分:0)

与所有内容一样,WPF会缩放线条的宽度,因此如果笔触宽度相对于几何体的范围太窄,则笔划可能会变得不可见。