BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Graphics
.NET 1.1+

Determine the Screen Resolution

Sometimes an application's user interface is laid out differently according to the resolution that is preferred by the user. For example, on small screen sizes, less information may be displayed. This tip explains how to obtain the current screen size.

The Screen Class

Within the System.Drawing namespace Microsoft have defined the Screen class. This class is used to represent display devices, including monitors, that can be used by Microsoft Windows. The Screen class includes various members including the Bounds property. The Bounds property returns a Rectangle object that contains information including the width and height of the display, measured in pixels.

The PrimaryScreen Property

Before the display size information can be extracted from the Screen class, a Screen object instance is required. The static PrimaryScreen property can be used to retrieve the details of the main display in use by windows. This object can then be interrogated as follows:

int height = Screen.PrimaryScreen.Bounds.Height;
int width = Screen.PrimaryScreen.Bounds.Width;

Multiple Monitors

For systems with multiple monitors attached, the PrimaryScreen property returns the details of the main display. To obtain the sizes of all of the displays, the Screen class contains another static property named AllScreens. This provides an array of displays with the primary display being item zero, the second display being index one, and so on. To determine the number of monitors available, simply check the Length property of the array.

int monitors = Screen.AllScreens.Length;
int height = Screen.AllScreens[0].Bounds.Height;
int width = Screen.AllScreens[0].Bounds.Width;
9 February 2008