【问题标题】:WPF DrawGeometry does not draw strokeWPF DrawGeometry 不绘制描边
【发布时间】:2014-02-13 14:01:07
【问题描述】:

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

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

这是我使用 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?
  • @Rui 我想看看生成几何图形的代码。图形段是否设置为描边?
  • 几何的范围是多少?我相信笔触宽度可能会缩小很多以适应它变得不可见的程度。
  • 我可能正在做一个比例转换,但我确实将笔画大小设置为 100 之类的东西。我确实将填充删除为空,看看我是否可以看到笔画没有运气.谢谢大家的帮助
  • @500 - 内部服务器错误 感谢一百万,我将 stoke 大小增加到 200,放大时我可以看到这条线。如果您发布为答案,我将标记为正确的:)

标签: c# wpf polygon stroke drawingcontext


【解决方案1】:

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

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

【讨论】:

  • 我在方法中添加了完整的代码长度,我确实将 PolyLineTo 属性 IsStroked 设置为 true。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 2013-12-22
    相关资源
    最近更新 更多