How can I collect and change screen resolution using Visual C#?
-
5Have you seen [this codeproject article](http://www.codeproject.com/KB/cs/csdynamicscrres.aspx)? – Justin Feb 22 '11 at 19:07
-
1@Justin: Add SystemInformation.PrimaryMonitorSize to the mix and post it as an answer. – Tedd Hansen Feb 22 '11 at 19:09
8 Answers
For retrieving the screen resolution, you're going to want to use the System.Windows.Forms.Screen
class. The Screen.AllScreens
property can be used to access a collection of all of the displays on the system, or you can use the Screen.PrimaryScreen
property to access the primary display.
The Screen
class has a property called Bounds
, which you can use to determine the resolution of the current instance of the class. For example, to determine the resolution of the current screen:
Rectangle resolution = Screen.PrimaryScreen.Bounds;
For changing the resolution, things get a little more complicated. This article (or this one) provides a detailed implementation and explanation.
-
2Is there a non-winforms way to do this? IE, something that would work with .net core? – Alexander Ryan Baggett Mar 19 '17 at 04:13
-
1i need the screen resolution for Desktop screen capture. After Zoomin it dosnt work it return Zoomed resolution so picture also zoomed any method for actual screen in Zoomin Window also . – Ahmad Jun 08 '18 at 11:00
This code will work perfectly in WPF. You can use it in either page load or in button click.
string screenWidth =System.Windows.SystemParameters.PrimaryScreenWidth.ToString();
string screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight.ToString();
txtResolution.Text ="Resolution : "+screenWidth + " X " + screenHeight;

- 179
- 1
- 3
In C# this is how to get the resolution Screen:
button click or form load:
string screenWidth = Screen.PrimaryScreen.Bounds.Width.ToString();
string screenHeight = Screen.PrimaryScreen.Bounds.Height.ToString();
Label1.Text = ("Resolution: " + screenWidth + "x" + screenHeight);
-
Very old question and answer - but this answer is now unhelpful as it doesn't describe a general solution in C#. I think this is winforms, not sure, but there are other frameworks and packages that might typically be used instead today. Really, these old questions need to be archived, or categorized to specific frameworks. – Gavin Williams Jul 11 '22 at 11:46
Answer from different solutions to get Display Resolution
Get the scaling factor
Get Screen.PrimaryScreen.Bounds.Width and Screen.PrimaryScreen.Bounds.Height multiple by scaling factor result
#region Display Resolution [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] public static extern int GetDeviceCaps(IntPtr hDC, int nIndex); public enum DeviceCap { VERTRES = 10, DESKTOPVERTRES = 117 } public static double GetWindowsScreenScalingFactor(bool percentage = true) { //Create Graphics object from the current windows handle Graphics GraphicsObject = Graphics.FromHwnd(IntPtr.Zero); //Get Handle to the device context associated with this Graphics object IntPtr DeviceContextHandle = GraphicsObject.GetHdc(); //Call GetDeviceCaps with the Handle to retrieve the Screen Height int LogicalScreenHeight = GetDeviceCaps(DeviceContextHandle, (int)DeviceCap.VERTRES); int PhysicalScreenHeight = GetDeviceCaps(DeviceContextHandle, (int)DeviceCap.DESKTOPVERTRES); //Divide the Screen Heights to get the scaling factor and round it to two decimals double ScreenScalingFactor = Math.Round(PhysicalScreenHeight / (double)LogicalScreenHeight, 2); //If requested as percentage - convert it if (percentage) { ScreenScalingFactor *= 100.0; } //Release the Handle and Dispose of the GraphicsObject object GraphicsObject.ReleaseHdc(DeviceContextHandle); GraphicsObject.Dispose(); //Return the Scaling Factor return ScreenScalingFactor; } public static Size GetDisplayResolution() { var sf = GetWindowsScreenScalingFactor(false); var screenWidth = Screen.PrimaryScreen.Bounds.Width * sf; var screenHeight = Screen.PrimaryScreen.Bounds.Height * sf; return new Size((int)screenWidth, (int)screenHeight); } #endregion
to check display resolution
var size = GetDisplayResolution();
Console.WriteLine("Display Resoluton: " + size.Width + "x" + size.Height);

- 239
- 4
- 4
-
How to get the scaling factor of a screen that is not the primary screen? – user1472131 Oct 03 '21 at 10:22
-
This is outstanding! However, can I suggest something? You're using GetDeviceCaps to get the PhysicalScreenHeight, then doing the same to get the LogicalScreenHeight, and dividing to get a ratio, and then following it up by multiplying by Screen.PrimaryScreen.Bounds ... but all you're ending up with is that PhysicalScreenHeight you already asked gdi32.dll about. Couldn't you just call it twice, once for PhysicalScreenHeight and another for PhysicalScreenWidth, and call it a day? – Kevin Nov 07 '21 at 17:15
To get the screen resolution the WindowsAppSDK (Microsoft.WindowsAppSDK on nuget) can get display information...
using Microsoft.UI.Windowing;
using Windows.Graphics;
...
RectInt32 region = DisplayArea.Primary.OuterBounds;
int w = region.Width;
int h = region.Height;

- 446
- 1
- 4
- 14
in Winforms, there is a Screen class you can use to get data about screen dimensions and color depth for all displays connected to the computer. Here's the docs page: http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.aspx
CHANGING the screen resolution is trickier. There is a Resolution third party class that wraps the native code you'd otherwise hook into. Use its CResolution nested class to set the screen resolution to a new height and width; but understand that doing this will only work for height/width combinations the display actually supports (800x600, 1024x768, etc, not 817x435).

- 70,210
- 21
- 112
- 164
If you want to collect screen resolution you can run the following code within a WPF window (the window is what the this
would refer to):
System.Windows.Media.Matrix m = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice;
Double dpiX = m.M11 * 96;
Double dpiY = m.M22 * 96;

- 126
- 4
-
1Question states: _"How can I collect and **change** screen resolution using Visual C#?"_ – Nov 15 '15 at 07:01
I have spent a lot of time trying to figure this out, for devices with scaling factor is very difficult to get the actual size of the screen due to the scaling factor sometimes 125% or 150% when the calls are made to the C# objects the returning value is not the right now, so you need to make a windows API call to get the scaling factor and apply the multiplier, the only working way that I have found for non WPF apps is here

- 136
- 2
- 8