Getting the size of a Windows Form

BigBug picture BigBug · Oct 30, 2011 · Viewed 68.9k times · Source

I'm creating a Windows Forms application. How do I capture the size of the windows form?

Currently I have something that looks like this in my code:

PictureBox display = new PictureBox();
display.Width = 360;
display.Height = 290;
this.Controls.Add(display);
display.Image = bmp;

However, the size of my display is hard-coded to a specific value.

I know that if I want to draw a square that re-sizes I can use something like this:

private Rectangle PlotArea;
private int offset = 30;
protected override void OnPaint(PaintEventArgs e)
{
    Graphics g = e.Graphics;

    //// Calculate the location and size of the plot area
    //// within which we want to draw the graphics:

    Rectangle ChartArea = ClientRectangle;
    PlotArea = new Rectangle(ChartArea.Location, ChartArea.Size);

    PlotArea.Inflate(-offset, -offset);

    Draw PlotArea:
    g.DrawRectangle(Pens.Black, PlotArea);
}

Is there a way for me to use method one and grab the size of the form for Height and Width?

I've tried the following code, but it doesn't work...

PictureBox display = new PictureBox();
display.Width = ClientRectangle.Y;
display.Height = ClientRectangle.X;
this.Controls.Add(display);
display.Image = bmp;

Answer

T30 picture T30 · Jul 29, 2013

For Getting the size of a Windows Form:

int formHeight = this.Height;
int formWidth = this.Width;