Getting the size of a Windows form

I am creating a Windows Forms application. How to get window shape size?

I currently have something similar to 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 hardcoded to a specific value.

I know that if I want to draw a square that resizes, 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 to use method one and capture the shape size for Height and Width?

I tried the following code but it does not work ...

 PictureBox display = new PictureBox(); display.Width = ClientRectangle.Y; display.Height = ClientRectangle.X; this.Controls.Add(display); display.Image = bmp; 
+4
source share
4 answers
  PictureBox display = new PictureBox(); display.Width = ClientRectangle.Width; display.Height = ClientRectangle.Height; this.Controls.Add(display); display.Image = bmp; 

when you resize the window

 private void Form1_Resize(object sender, System.EventArgs e) { Control control = (Control)sender; //set the PictureBox width and heigh here using control.Size.Height // and control.Size.Width } 
+7
source

To get the size of a Windows form:

 int formHeight = this.Height; int formWidth = this.Width; 
+9
source

You want to set Dock = DockStyle.Fill to Dock = DockStyle.Fill control in order to populate its parent element.

+4
source

Set the value to ClientRectangle.Width .

+2
source

All Articles