DrawGeometry笔划不准确

时间:2014-07-18 11:54:28

标签: c# wpf render

我正在进行自定义控制,它的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);
    }

为什么中风是 innacurate ?在屏幕截图中,您可以看到白色笔划超出控制范围

enter image description here

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

我可以根据校正尺寸计算几何图形,但我不知道如何计算笔划图。

如何使DrawGeometry在控制范围内适合中风?或者也许还有其他方法可以将轮廓绘制成复杂形状的数字?

1 个答案:

答案 0 :(得分:2)

笔总​​是精确地绘制在几何图形的中间。因此,对于厚度为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));