【问题标题】:Create Rectangle from Left,Right,Top and Bottom从左、右、上和下创建矩形
【发布时间】:2021-05-12 16:48:47
【问题描述】:

我有一个列表,其中包含矩形的左、右、底部和顶部。

如何将其转换为 Rectangle[] 数组?

【问题讨论】:

标签: c# .net system.drawing


【解决方案1】:

定义矩形

Rectangle(left,top,width,height)
Rectangle[] rectangles = new Rectangle[2]
{
    new Rectangle(0,0,100,50),
    new Rectangle(200,100,200,50),
};



List<Rectangle> rectangles = new List<Rectangle>();
Rectangle rect = new Rectangle();
rect.X = 5;
rect.Y = 10;
rect.Width = 100;
rect.Height = 50;
rectangles.Add(rect);

获取底部和右侧的高度和宽度

int left = 100;
int top = 50;
int right = 400;
int bottom = 250;

int width = right - left;
int heigth = bottom - top;
Rectangle rect = new Rectangle(left, top, width, heigth);

绘制矩形数组

private void DrawRectangle()
{
    Rectangle[] rectangles = new Rectangle[]
    {
       new Rectangle(0,0,100,50),
       new Rectangle(200,100,200,50),
       new Rectangle(300,200,160,60)
    };
    foreach(var rect in rectangles)
    {
       System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);
       System.Drawing.Graphics formGraphics;
       formGraphics = this.CreateGraphics();
       formGraphics.FillRectangle(myBrush, rect);
       myBrush.Dispose();
       formGraphics.Dispose();
    }
}

【讨论】:

  • 感谢您的努力,但这可以使用 cmets 中提到的一行代码来实现。
【解决方案2】:

试试这个: Rectangle.FromLTRB(left, right, top, bottom);

【讨论】:

    猜你喜欢
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多