Click here to Skip to main content
15,885,537 members
Articles / Programming Languages / C#
Tip/Trick

How to get screen resolution using API in C#

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
11 Jun 2013CPOL 19.9K   3   5   2
Geting screen resolution using API with the possibility to recognize virtual screens.

Introduction 

This article describe how to get screen resolution using API functions.

Background 

When you need screen resolution in .NET, you can use the Screen.AllScreens property. It works fine, but sometimes you have virtual screens and using the SystemInformation.VirtualScreen property, you can get the resolution, but you cannot recognize which screen is virtual. I tried to find it using WMI, but it stops working when you change the orientation to portrait - this screen is excluded from the list. The best way (for me) is to use API functions - just these two functions:

C#
[DllImport("user32.dll")]
static extern bool EnumDisplayDevices(string lpDevice, uint iDevNum, 
       ref DISPLAY_DEVICE lpDisplayDevice, uint dwFlags);
[DllImport("user32.dll")]
public static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);

Using this way you can get the rotation of the screen too. It was a long way for me to find it and I hope it helps you Smile

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Czech Republic Czech Republic
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionFurther explanations please Pin
DennisDo9-Jul-13 21:52
DennisDo9-Jul-13 21:52 
GeneralMy vote of 3 Pin
Daniele Rota Nodari11-Jun-13 1:55
Daniele Rota Nodari11-Jun-13 1:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.