矩形可以按行和列分割吗?

时间:2014-12-06 10:13:37

标签: c# image-processing graphics drawing rectangles

          private void panel1_Paint(object sender, PaintEventArgs e)
    {
        Pen mypen = default(Pen);
        mypen = new Pen(System.Drawing.Color.Red, 3);
        mypen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
        //For Dash Line in Rectangle 
        Pen mypen1 = default(Pen);
        mypen1 = new Pen(System.Drawing.Color.Blue, 1);
        mypen1.DashStyle![enter image description here][2] = System.Drawing.Drawing2D.DashStyle.Dash;

        //Pen mypen2 =default(Pen);
        //mypen2 = new Pen(System.Drawing.Color.Yellow, 3);
        //mypen2.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

        //Pen mypen3 = default(Pen);
        //mypen3 = new Pen(System.Drawing.Color.Violet, 1);
        //mypen3.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

        L1 = Rect1LT.X + 5;
        T1 = Rect1LT.Y + 5;
        W1 = Rect1RB.X - Rect1LT.X;
        H1 = Rect1RB.Y - Rect1LT.Y;


        e.Graphics.DrawRectangle(mypen, new System.Drawing.Rectangle(L1, T1, W1, H1));
        e.Graphics.DrawRectangle(mypen1, new System.Drawing.Rectangle(L1, T1, W1, H1));

        e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1LT.X, Rect1LT.Y, 10, 10));
        e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1RT.X, Rect1RT.Y, 10, 10));
        e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1LB.X, Rect1LB.Y, 10, 10));
        e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1RB.X, Rect1RB.Y, 10, 10));

        e.Graphics.FillRectangle(Brushes.Blue, Rect1LT);
        e.Graphics.FillRectangle(Brushes.Blue, Rect1RT);
        e.Graphics.FillRectangle(Brushes.Blue, Rect1LB);
        e.Graphics.FillRectangle(Brushes.Blue, Rect1RB);

        e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1T.X, Rect1T.Y, 10, 10));
        e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1R.X, Rect1R.Y, 10, 10));
        e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1B.X, Rect1B.Y, 10, 10));
        e.Graphics.DrawRectangle(Pens.Blue, new System.Drawing.Rectangle(Rect1L.X, Rect1L.Y, 10, 10));

        e.Graphics.FillRectangle(Brushes.Blue, Rect1T);
        e.Graphics.FillRectangle(Brushes.Blue, Rect1R);
        e.Graphics.FillRectangle(Brushes.Blue, Rect1B);
        e.Graphics.FillRectangle(Brushes.Blue, Rect1L);

        DataGridView dg1 = new DataGridView();


        //while (!exit)
        //{
        //    var time = GetTime();
        //    Update(time);
        //    Render(time);
        //}


    }

我想在行和列中划分矩形,行和列的大小可以在运行时更改?行和列的数量也可以更改?我不想分割整个矩形我只想将它们分成矩形。

1 个答案:

答案 0 :(得分:2)

这是一个简单的例子:

int cols = 7; int rows = 11;

private void panel1_Paint(object sender, PaintEventArgs e)
{
    Rectangle Rect = // your Rectangle!

    Rectangle pRect = Rect;  // panel2.ClientRectangle;
    float width = 1f * pRect.Width / cols;
    float height = 1f * pRect.Height / rows;

    using (Pen pen = new Pen(System.Drawing.Color.Blue, 1))
    {
      pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

      for (int c = 0; c < cols; c++)
         for (int r = 0; r < rows; r++)
         {
            RectangleF rect = new RectangleF(pRect.X + c * width, pRect.Y + r * height, 
                                             width, height);
            e.Graphics.FillRectangle(Brushes.Coral, rect);
            // e.Graphics.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height);
         }

        for (int c = 0; c < cols; c++) e.Graphics.DrawLine(pen, 
             pRect.X + c * width, pRect.Y, pRect.X + c * width, pRect.Y + pRect.Height);
        for (int r = 0; r < rows; r++) e.Graphics.DrawLine(pen, 
             pRect.X, pRect.Y + r * height, pRect.X + pRect.Width, pRect.Y + r * height);
        e.Graphics.DrawRectangle(Pens.Red, 
                                 pRect.X, pRect.Y, pRect.Width - 1, pRect.Height - 1);

    }
}

请注意一些变化:

  • 边框颜色和填充颜色不能相同。
  • 必须先填充,否则会覆盖边框
  • 我使用花车完全填充面板;如果cols和rows没有均匀地划分为面板/矩形大小,则重新整理将不会都具有相同的大小..
  • 起初我忽略了你的DashStyle。如果你想拥有DashStyle,你必须完全改变你的计划!原因是如果你在网格中绘制矩形,那么......

    • ..要么让它们重叠,那么破折号将以彼此的方式进入。有一个DashOffset参数,但我认为它不能被扭曲以使其适用于任何网格。

    • ..或者您需要在网格单元格内绘制矩形,但是它们的厚度是两倍,图案仍会相互干扰。

相反,您只需绘制几行,如图所示!

这是我与Dashes的例子:

enter image description here