Get the screen’s resolution in C#

[resolution]

Normally a monitor displays 96 logical (or “notional”) dots per inch (dpi) but it’s not necessarily a good idea to assume that value. When this example starts, the following code displays the logical resolution of the screen in dpi.

private void Form1_Load(object sender, EventArgs e)
{
    using (Graphics gr = this.CreateGraphics())
    {
        txtScreenHorizontal.Text = gr.DpiX.ToString() + " dpi";
        txtScreenVertical.Text = gr.DpiY.ToString() + " dpi";
    }
    txtScreenHorizontal.Select(0, 0);
}

The code creates a Graphics object and then uses its DpiX and DpiY properties to get the horizontal and vertical resolution.

Note that logical dots per inch is not necessarily the same as actual dots per inch on the screen. That will also depend on the screen size and the resolution that you have selected. For example, my 15″ screen can use resolutions between 800 x 600 pixels and 1366 x 768 pixels but they all report 96 dpi.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in drawing, graphics and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *