【问题标题】:WPF Canvas - Single Pixel GridWPF 画布 - 单像素网格
【发布时间】:2010-05-26 20:24:14
【问题描述】:

我有一个自定义 WPF 画布,我想在其上显示一个网格。我通过覆盖 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/zoom ?
  • 谢谢!好像暂时想不出来。
  • 感谢上面的代码。从过去的 2 天开始,我一直在寻找这种类型的示例。我已经实现了一个自定义画布并绘制了如图所示的网格。问题是它非常慢。当我最大化或调整窗口大小时,性能真的很慢。它有点悬。有什么解决办法吗?

标签: wpf drawing canvas pixel


【解决方案1】:

作为原始帖子状态的cmets。笔粗细应设置为 1.0/zoom。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 2014-10-06
    相关资源
    最近更新 更多