Click here to Skip to main content
11,812,712 members (73,266 online)
Click here to Skip to main content

Dynamic Screen Resolution

, 22 Jul 2008 CPOL 304K 11.5K 91
Rate this:
Please Sign up or sign in to vote.
Setting the Screen Resolution in C#


I had a hard time in setting a dynamic screen resolution when I was doing some thick client application for a test suite. I presume that most of us have had this happen to us, or have faced such times somewhere along our engineering life cycle. Because, as we all know, the resolution of the user screen may not be same as that of the development environment screen.

This article expects to deliver what I found as a solution for the discussed challenge.

The forthcoming part of this literature will communicate about:

  • How to get the end user screen resolution
  • How to change the user screen resolution to product compatible
  • How to safeguard the user screen resolution

How to Get the End User Screen Resolution

Accessing the user screen is being eased by the availing Screen class which is shipped along with the .NET framework. And can access the current user screen resolution through a static Screen.PrimaryScreen property available in the Screen class.

public static Screen PrimaryScreen {get;}

The aforementioned property is read only and returns a Screen type. And the below given logic demonstrates how to make use of the Screen type to access the user screen resolution.

Screen screen = Screen.PrimaryScreen;
int S_width=screen.Bounds.Width;
int S_height=screen.Bounds.Height;

How to Change the User Screen Resolution to Product Compatible

Before heading towards our next goal, let me talk about the unmanaged part of this implementation. Unlike traditional languages, the .NET framework holds a distinct step while leveraging both managed and unmanaged code. Personally, when I wrote this article, I never found any managed code which does this resolution treatment. And this is what made me think to explore some Win32 API’s.

Before continuing, I would request you to have hands on or theoretical knowledge about: COM Interop service, Attribute, DLL import attribute and Platform Invoke.

Since the scope of this article is limited to managed code, I will not be discussing anything about unmanaged code. But, despite that fact, we can use the DllImport attribute to read the definition of unmanaged code to your managed environment. In this case, we will be using the User32.dll API, which facilitates the dynamic resolution and has two functions related to changing the screen resolution.

  • EnumDisplaySettings
  • ChangeDisplaySettings
class User32
        public static extern int EnumDisplaySettings (
          string deviceName, int modeNum, ref DEVMODE devMode );         
        public static extern int ChangeDisplaySettings(
              ref DEVMODE devMode, int flags); 
        public const int ENUM_CURRENT_SETTINGS = -1;
        public const int CDS_UPDATEREGISTRY = 0x01;
        public const int CDS_TEST = 0x02;
        public const int DISP_CHANGE_SUCCESSFUL = 0;
        public const int DISP_CHANGE_RESTART = 1;
        public const int DISP_CHANGE_FAILED = -1;

As we know, the [DllImport("user32.dll")] is an explicit groundwork before injecting an unmanaged implementation in our managed environment.

public static extern int EnumDisplaySettings 
    (string deviceName, int modeNum, ref DEVMODE devMode);

DEVMODE is a structure that is explained in the platform documentation as well as included with Visual Studio .NET. The structure is defined in C#.

public struct DEVMODE 
          public string dmDeviceName;
        public short  dmSpecVersion;
        public short  dmDriverVersion;

You can have a look at the source code attached and apply this logic to any event that you want.

Be aware that, the types have the right size and that fixed length strings are appropriately defined. In this case WORD maps to short, DWORD to int and short stays as short.

dm.dmDeviceName = new String (new char[32]);
dm.dmFormName = new String (new char[32]);
dm.dmSize = (short)Marshal.SizeOf (dm); 
if (0 != User32.EnumDisplaySettings (null, 
       User32.ENUM_CURRENT_SETTINGS, ref dm))

At this point the DEVMODE structure will be decorated with the default settings and can modify it at any instance.

dm.dmPelsWidth = iWidth;
dm.dmPelsHeight = iHeight; 
int iRet = User32.ChangeDisplaySettings (
  ref dm, User32.CDS_UPDATEREGISTRY);

The enclosed code block does this a little differently to deal with various error conditions. I would encourage you to look at the full source file and see what it does. That's all there is to it.

How to Safeguard the User Screen Resolution

Finally, before we continue, it’s our responsibly to safeguard a user's default screen resolution. In order to do that, you might have to use some static members or classes to hold the user screen resolution and retain it back when you complete the execution.

See the downloads at the top of this article for the source code.


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


About the Author

sreejith ss nair
Software Developer (Senior) Freelance
India India
He is a certified professional in both MCPD and MCTS. He is a mathematics graduate with masters in computer science.He was born and bred in India and happen to spend some time in Europe.

You may also be interested in...

Comments and Discussions

QuestionAny resolution Pin
Computer Wiz9912-May-15 4:30
memberComputer Wiz9912-May-15 4:30 
QuestionError with windows 7 - resolution 1024 x 768 Pin
Member 123577420-Aug-14 8:54
memberMember 123577420-Aug-14 8:54 
AnswerRe: Error with windows 7 - resolution 1024 x 768 (resolved) Pin
Member 123577421-Aug-14 3:29
memberMember 123577421-Aug-14 3:29 
Questionthank's Pin
hoseinziba23-Mar-14 1:31
memberhoseinziba23-Mar-14 1:31 
QuestionSet screen reslolution Pin
Sasha Neftin4-Mar-14 19:35
memberSasha Neftin4-Mar-14 19:35 
Questiondown Pin
little mantou10-Jul-12 22:50
memberlittle mantou10-Jul-12 22:50 
QuestionHigh Resolutions Pin
ahmed.helmy20418-Dec-11 3:17
memberahmed.helmy20418-Dec-11 3:17 
QuestionCannot change resolution in Winlogon Screen Pin
sonali.mendis8-Sep-11 21:04
membersonali.mendis8-Sep-11 21:04 
QuestionNice its helps me.. Pin
Pritesh Aryan25-Jul-11 21:01
memberPritesh Aryan25-Jul-11 21:01 
GeneralMy vote of 5 Pin
Member 23125037-Jul-11 0:21
memberMember 23125037-Jul-11 0:21 
GeneralMy vote of 5 Pin
tomydragon5-Oct-10 21:52
membertomydragon5-Oct-10 21:52 
GeneralDesktop doesn't follow new orientation on Windows 7 Pin
rapik212-Apr-09 2:24
memberrapik212-Apr-09 2:24 
GeneralDual Monitor Support Pin
Daniel N21-Apr-09 1:55
memberDaniel N21-Apr-09 1:55 
GeneralThanks for this usefull post Pin
Medoune9-Mar-09 4:40
memberMedoune9-Mar-09 4:40 
GeneralDynamic Screen Resoultion for Windows Mobile Pin
jayanice24-Feb-09 17:51
memberjayanice24-Feb-09 17:51 
Question1280x800 ?? Pin
ronaldo268920-Nov-08 12:28
memberronaldo268920-Nov-08 12:28 
Generalconstant size of letters whatever the screen physical size Pin
pierre_lannion14-Sep-08 22:44
memberpierre_lannion14-Sep-08 22:44 
AnswerRe: constant size of letters whatever the screen physical size Pin
webatxcent15-Jan-09 8:18
memberwebatxcent15-Jan-09 8:18 
Generalget available screen resolutions Pin
pravin parmar15-Oct-07 19:32
memberpravin parmar15-Oct-07 19:32 
GeneralRe: get available screen resolutions Pin
sreejith ss nair16-Oct-07 0:15
membersreejith ss nair16-Oct-07 0:15 
GeneralRe: get available screen resolutions Pin
pravin parmar16-Oct-07 18:37
memberpravin parmar16-Oct-07 18:37 
GeneralRe: get available screen resolutions Pin
SKRaj7822-Jan-08 9:29
memberSKRaj7822-Jan-08 9:29 
GeneralRe: get available screen resolutions Pin
SKRaj7822-Jan-08 10:10
memberSKRaj7822-Jan-08 10:10 
GeneralRe: get available screen resolutions Pin
SKRaj7822-Jan-08 10:56
memberSKRaj7822-Jan-08 10:56 
GeneralRe: get available screen resolutions Pin
IgorAfonin25-Jul-12 4:22
memberIgorAfonin25-Jul-12 4:22 
GeneralRe: get available screen resolutions Pin
izne31-Mar-08 16:45
memberizne31-Mar-08 16:45 
QuestionCDS_TEST is 0x2 and CDS_UPDATEREGISTRY = 0x1??? Pin
AYR Chen17-Aug-07 21:08
memberAYR Chen17-Aug-07 21:08 
GeneralError Message Pin
megaroo7926-Jun-07 14:47
membermegaroo7926-Jun-07 14:47 
GeneralRe: Error Message Pin
shradds18-Mar-09 4:29
membershradds18-Mar-09 4:29 
GeneralRe: Error Message Pin
Epon14-Jul-10 19:57
memberEpon14-Jul-10 19:57 
GeneralRe: Error Message Pin
Member 1010100917-Aug-13 21:45
memberMember 1010100917-Aug-13 21:45 version Pin
nandhucbm5-Jun-07 2:55
membernandhucbm5-Jun-07 2:55 
Generalplease help me regarding use u,r logic in Pin
ram krishna pattnayak13-Feb-07 21:08
memberram krishna pattnayak13-Feb-07 21:08 
GeneralRe: please help me regarding use u,r logic in Pin
sreejith ss nair11-Mar-07 23:13
membersreejith ss nair11-Mar-07 23:13 
GeneralUseful tip Pin
Vyacheslav Trubarov13-Jan-07 11:25
memberVyacheslav Trubarov13-Jan-07 11:25 
GeneralRe: Useful tip Pin
SKRaj7822-Jan-08 9:19
memberSKRaj7822-Jan-08 9:19 
Generalhiii sreejith Pin
kannan m24-Jul-06 3:52
memberkannan m24-Jul-06 3:52 
GeneralRe: hiii sreejith Pin
sreejith ss nair7-Aug-06 4:41
membersreejith ss nair7-Aug-06 4:41 
AnswerRe: hiii sreejith Pin
John Storer II14-Nov-06 2:21
memberJohn Storer II14-Nov-06 2:21 
GeneralThanks a BUNCH :-) Pin
KJAM200513-Jul-06 0:14
memberKJAM200513-Jul-06 0:14 
GeneralRe: Thanks a BUNCH :-) Pin
sreejith ss nair7-Aug-06 4:42
membersreejith ss nair7-Aug-06 4:42 
GeneralNew and GRAND problem - Disorder Desktop-Icons Pin
sevenverse6-Jun-06 0:05
membersevenverse6-Jun-06 0:05 
GeneralRe: New and GRAND problem - Disorder Desktop-Icons Pin
sreejith ss nair7-Aug-06 4:43
membersreejith ss nair7-Aug-06 4:43 
Questionproblem - Disorder Desktop-Icons Pin
xDarkmanx4-Mar-08 11:56
memberxDarkmanx4-Mar-08 11:56 
GeneralGood Idea for small games BUT HOW DOW I GET THE SUPPORTED RESOLUTION Pin
sevenverse5-Jun-06 23:35
membersevenverse5-Jun-06 23:35 
GeneralRe: Good Idea for small games BUT HOW DOW I GET THE SUPPORTED RESOLUTION Pin
sreejith ss nair7-Aug-06 4:47
membersreejith ss nair7-Aug-06 4:47 
GeneralGreat Idea, wrong suggestion Pin
El Gato10-May-06 8:36
memberEl Gato10-May-06 8:36 
GeneralRe: Great Idea, wrong suggestion Pin
sreejith ss nair7-Aug-06 4:45
membersreejith ss nair7-Aug-06 4:45 
QuestionRe: Great Idea, wrong suggestion Pin
sreejith ss nair11-Mar-07 23:22
membersreejith ss nair11-Mar-07 23:22 
AnswerRe: Great Idea, wrong suggestion Pin
El Gato13-Mar-07 8:32
memberEl Gato13-Mar-07 8:32 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151002.1 | Last Updated 22 Jul 2008
Article Copyright 2004 by sreejith ss nair
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid