techno
techno

Reputation: 6500

Create Rectangle from Left,Right,Top and Bottom

I have a list that contains left,right,bottom and top of rectangles.

enter image description here

How can i cast this to a Rectangle[] array?

Upvotes: 2

Views: 5543

Answers (2)

crlanglois
crlanglois

Reputation: 3609

Try this: Rectangle.FromLTRB(left, right, top, bottom);

Upvotes: 0

persian-theme
persian-theme

Reputation: 6638

Define Rectangle

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);

Obtain height and width with bottom and right

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);

Draw Rectangle Array

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();
    }
}

Upvotes: 3

Related Questions