WPF Canvas - 单像素网格

时间:2010-05-26 20:24:15

标签: wpf drawing canvas pixel

我有一个自定义的WPF Canvas,我想在其上显示一个网格。我这样做是通过覆盖Canvas上的OnRender方法,并使用DrawingContext绘图函数。 IsGridVisible,GridWidth,GridHeight分别是每个网格线水平和垂直之间的像素数。

我还使用Canvas.LayoutTransform属性上的ScaleTransform来缩放Canvas项目,并且正如人们所期望的那样,网格线厚度乘以ScaleTransform缩放因子,如下图所示。有没有办法绘制单个像素线,无论当前的Canvas RenderTransform?

    protected override void OnRender(System.Windows.Media.DrawingContext dc)
    {
        base.OnRender(dc);

        if (IsGridVisible)
        {
            // Draw GridLines
            Pen pen = new Pen(new SolidColorBrush(GridColour), 1);
            pen.DashStyle = DashStyles.Dash;

            for (double x = 0; x < this.ActualWidth; x += this.GridWidth)
            {
                dc.DrawLine(pen, new Point(x, 0), new Point(x, this.ActualHeight));
            }

            for (double y = 0; y < this.ActualHeight; y += this.GridHeight)
            {
                dc.DrawLine(pen, new Point(0, y), new Point(this.ActualWidth, y));
            }
        }
    }

alt text http://www.freeimagehosting.net/uploads/f05ad1f602.png

1 个答案:

答案 0 :(得分:2)

作为对原始帖子状态的评论。笔的厚度应设置为1.0 / zoom。