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).
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:
For changing the resolution, things get a little more complicated. This article (or this one) provides a detailed implementation and explanation. Hope this helps.
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
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);
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;