【问题标题】:DrawGeometry stroke is innacurate绘制几何笔画不准确
【发布时间】:2014-07-18 11:54:28
【问题描述】:

我正在制作自定义控件,它的 Render 看起来像这样

protected override void OnRender(DrawingContext context)
{
    var geometry = new PathGeometry(new[] { new PathFigure(new Point(0, 0), new [] {
        new LineSegment(new Point(ActualWidth, 0), true),
        new LineSegment(new Point(ActualWidth, ActualHeight), true),
        new LineSegment(new Point(0, ActualHeight), true),
    }, true) });
    context.DrawGeometry(new LinearGradientBrush(Colors.White, Colors.Black, 90),
        new Pen(Brushes.White, 10), geometry);
    }

为什么中风如此不准确?在屏幕截图中,您可以看到白色笔划超出控制范围

我不能简单地用ClipToBounds 剪辑它,因为我需要准确地绘制并且希望使用笔触而不是手动绘制轮廓(图形比矩形复杂得多)。

我可以根据校正后的大小计算几何图形,但我不知道如何计算笔画。

如何使DrawGeometry 适合控制范围内的笔划?或者也许有其他方法可以为复杂形状的图形绘制轮廓?

【问题讨论】:

    标签: c# wpf render


    【解决方案1】:

    笔总​​是精确地绘制在几何轮廓的中间。因此,使用厚度为 10 的 Pen,您必须创建边距为 5 像素的几何体:

    context.DrawRectangle(
        new LinearGradientBrush(Colors.White, Colors.Black, 90),
        new Pen(Brushes.White, 10),
        new Rect(5, 5, ActualWidth - 10, ActualHeight - 10));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-26
      • 2014-07-06
      • 2014-10-28
      • 1970-01-01
      • 2021-01-28
      相关资源
      最近更新 更多