【问题标题】:Why my coordinate (1,1) start at (0, 1)?为什么我的坐标 (1,1) 从 (0, 1) 开始?
【发布时间】:2014-06-22 08:29:20
【问题描述】:

我覆盖了边界控制并在我覆盖的 OnRender 中执行:

protected override void OnRender(System.Windows.Media.DrawingContext dc)
        {
            this.SnapsToDevicePixels = true;
            this.VisualEdgeMode = EdgeMode.Aliased;
            var myPen = new Pen(new SolidColorBrush(Colors.LightGray), 1);
            dc.DrawLine(myPen, new Point(1, 1), new Point(1, RenderSize.Height - 1));
            return;

这给了我这个结果:

问题:

有谁能告诉我为什么我的代码画了一条从 (0,1) 开始的线,而它假设从 (1, 1) 开始,就像用代码写的那样?

我的 DPI 是 96、96。

对于 ref,这是我的 xaml:

<Window xmlns:MyControls="clr-namespace:MyControls;assembly=MyControls"  x:Class="TestControls.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>


        <MyControls:Border3D BorderThickness="3" BorderBrush="Aqua">
            <Rectangle Width="74" Height="3" HorizontalAlignment="Left">
                <Rectangle.Fill>
                    <SolidColorBrush Color="#40FF0000">

                    </SolidColorBrush>
                </Rectangle.Fill>
            </Rectangle>
        </MyControls:Border3D>

        <Rectangle  Grid.Row="1" Grid.Column="0" Width="80" Grid.ColumnSpan="2" HorizontalAlignment="Left">
            <Rectangle.Fill>
                <SolidColorBrush Color="LightGray"></SolidColorBrush>
            </Rectangle.Fill>
        </Rectangle>

    </Grid>
</Window>

【问题讨论】:

  • 如果将宽度更改为 526,还会出现这种情况吗?
  • 看来-我猜-您没有计算边界线..所以当您尝试绘制 (1,1) 时,您会在屏幕上看到 (0,1).. 试试这个: new Point (1+border.TopLine.Height, 1+border.LeftWall.width) 注意:给定的代码是 PSEUDO,所以在 .NET 中可能找不到同名的方法/字段
  • 谢谢。克莱门斯似乎对我的回答有确切的答案,我会说一点很好的额外解释:-)!

标签: c# wpf drawing line render


【解决方案1】:

请记住,(0, 0) 不是左上角像素的中心。相反,它是该像素的左上角。为了在第二个像素列(索引为 1)的中间绘制笔画粗细为 1 的线条,您必须从 (1.5, 1) 绘制到 (1.5, RenderSize.Height - 1)

dc.DrawLine(myPen, new Point(1.5, 1), new Point(1.5, RenderSize.Height - 1));

设置SnapsToDevicePixels = true 使您的线条向左对齐半个像素。


如果您将PenLineCap.Square 用于线条笔的StartLineCapEndLineCap 属性,则可以从一个像素中心精确地绘制到另一个像素中心:

var myPen = new Pen(Brushes.LightGray, 1);
myPen.StartLineCap = PenLineCap.Square;
myPen.EndLineCap = PenLineCap.Square;
dc.DrawLine(myPen, new Point(1.5, 1.5), new Point(1.5, RenderSize.Height - 1.5));

【讨论】:

  • 非常感谢克莱门斯。很干净的解释。我想知道您是否有建议阅读一本书以了解 WPF 绘图的各个方面的微妙之处?
  • Adam Nathan 的 WPF Unleashed 有一个关于 2D 图形的综合章节,它应该为您提供一个很好的起点。 MSDN上也有几篇详细的文章。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
  • 2011-02-16
  • 2023-03-22
相关资源
最近更新 更多