代码之家  ›  专栏  ›  技术社区  ›  Luke Vo

在GDI+中绘制具有更好性能的网格

  •  0
  • Luke Vo  · 技术社区  · 11 年前

    我需要画一个网格 Graphics 类(刚刚越界),以及 透明表示法 喜欢这张照片:

    enter image description here

    除了画每一条线/每一个矩形,我不知道还有什么别的办法。如果场地很宽,性能就很差。有没有更好的方法来画这些东西?

    以下是我当前绘制网格的代码:

        private void drawGrid(Graphics pGraphic, int pGridSize)
        {
            int verticalCount = this.mPicScreen.Width / pGridSize + 1;
            int horizontalCount = this.mPicScreen.Height / pGridSize + 1;
    
            Pen p = new Pen(Color.Gray);
    
            // Vertical Lines
            for (int i = 0; i < verticalCount; i++)
            {
                pGraphic.DrawLine(p,
                    new Point(i * pGridSize, 0),
                    new Point(i * pGridSize, this.mPicScreen.Height));
            }
    
            // Horizontal Lines
            for (int i = 0; i < horizontalCount; i++)
            {
                pGraphic.DrawLine(p,
                    new Point(0, i * pGridSize),
                    new Point(this.mPicScreen.Width, i * pGridSize));
            }
        }
    
    2 回复  |  直到 11 年前
        1
  •  3
  •   user287107    11 年前

    有一个更好的方法:只用刷子

    如果需要制作用户定义的网格大小,可以使用

    System.Drawing.TextureBrush 
    

    如果一个随机的是可以的,你可以在

    System.Drawing.Drawing2D.HatchBrush 
    

    已经是网格样式

        2
  •  0
  •   Luke Vo    11 年前

    控件在绘制过程中实际上是闪烁的(因为控件是故意无效的)。这不是性能问题,但实际上是因为控制 DoubleBuffered

    以下是要启用的代码 双重缓冲 在…上 Controls 以下为:

    public static void setDoubleBuffered(System.Windows.Forms.Control c)
    {
        if (System.Windows.Forms.SystemInformation.TerminalServerSession)
            return;
    
        System.Reflection.PropertyInfo aProp =
              typeof(System.Windows.Forms.Control).GetProperty(
                    "DoubleBuffered",
                    System.Reflection.BindingFlags.NonPublic |
                    System.Reflection.BindingFlags.Instance);
    
        aProp.SetValue(c, true, null);
    }