Get and Set Screen Resolution

How to get display resolution (screen size)?

_ScreenWidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
_ScreenHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;

extra references: System.Drawing , System.Windows.Forms

Get screen resolution as a variable in cmd

You can even get more parameters with one single wmic:

for /f %%i in ('wmic desktopmonitor get screenheight^,screenwidth /value ^| find "="') do set "%%f"
echo your screen is %screenwidth% * %screenheight% pixels

If you need to have your own variablenames, it's a bit more complicated:

for /f "tokens=1,2 delims==" %%i in ('wmic desktopmonitor get screenheight^,screenwidth /value ^| find "="') do (
if "%%i"=="ScreenHeight" set height=%%j
if "%%i"=="ScreenWidth" set width=%%j
)
echo your screen is %width% * %height% pixels

if you need only one value:

for /f "tokens=2 delims==" %%i in ('wmic desktopmonitor get screenheight /value ^| find "="') do set height=%%i
echo %height%

How to get monitor/display resolution

Looks like i found solution. @Draco18s pointed to Screen class but what i'm looking for is called Display. Display class allows me to get native size of monitor or display in pixels. Looks like "Screen" class is used only for game window/full screen but for display as hardware device is used "Display" class. It was added to Unity3d very recently with multi-display support, this is reason why very few people knows about its existence.

To be more specific i was looking for Display.main.systemHeight and Display.main.systemWidth

Get screen resolution in console app

You can use the System.Windows namespace, in the SystemParameters class you have the following properties:

PrimaryScreenWidth

PrimaryScreenHeight

I believe someone made a reference answering this question: Get and Set Screen Resolution

But you will have to add the PresentationFramework.dll to your console project.

using System.Windows;

namespace DispResolution
{
class Program
{
static void Main(string[] args)
{
double height = SystemParameters.PrimaryScreenHeight;
double Width = SystemParameters.PrimaryScreenWidth;
}
}
}

How to get the resolution of the screen in which a user control is opened?

You can accomplish it by various ways but I like straightforward way using MonitorFromRect and GetMonitorInfo functions. Assuming you set proper app.manifest for Per-Monitor DPI, the following extension method will work to get screen Rect from a FrameworkElement.

Regarding the point jwdonahue raised, this method will return the Rect of screen which has the largest intersection with the specified FrameworkElement.

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Media;

public static class ScreenHelper
{
public static Rect ToScreenRect(this FrameworkElement element) => GetMonitorRect(GetElementRect(element));

private static Rect GetElementRect(FrameworkElement element)
{
var point = element.PointToScreen(new Point(0, 0));
var dpi = VisualTreeHelper.GetDpi(element);
return new Rect(point, new Size(element.ActualWidth * dpi.DpiScaleX, element.ActualHeight * dpi.DpiScaleY));
}

private static Rect GetMonitorRect(Rect elementRect)
{
RECT rect = elementRect;
var monitorHandle = MonitorFromRect(ref rect, MONITOR_DEFAULTTONULL);
if (monitorHandle != IntPtr.Zero)
{
var monitorInfo = new MONITORINFO { cbSize = (uint)Marshal.SizeOf<MONITORINFO>() };
if (GetMonitorInfo(monitorHandle, ref monitorInfo))
{
return monitorInfo.rcMonitor;
}
}
return Rect.Empty;
}

[DllImport("User32.dll")]
private static extern IntPtr MonitorFromRect(ref RECT lprc, uint dwFlags);

private const uint MONITOR_DEFAULTTONULL = 0x00000000;

[DllImport("User32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);

[StructLayout(LayoutKind.Sequential)]
private struct MONITORINFO
{
public uint cbSize;
public RECT rcMonitor;
public RECT rcWork;
public uint dwFlags;
}

[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;

public static implicit operator Rect(RECT rect)
{
return new Rect(
rect.left,
rect.top,
rect.right - rect.left,
rect.bottom - rect.top);
}

public static implicit operator RECT(Rect rect)
{
return new RECT
{
left = (int)rect.X,
top = (int)rect.Y,
right = (int)rect.Right,
bottom = (int)rect.Bottom
};
}
}
}

How can I get screen resolution in java?

You can get the screen size with the Toolkit.getScreenSize() method.

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();

On a multi-monitor configuration you should use this :

GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();

If you want to get the screen resolution in DPI you'll have to use the getScreenResolution() method on Toolkit.


Resources :

  • javadoc - Toolkit.getScreenSize()
  • Java bug 5100801- Toolkit.getScreenSize() does not return the correct dimension on multimon, linux

Get current screen resolution using .NET Core Console Application

After installing NuGet package System.Management, you can access these parameters as explained here:

    //  https://wutils.com/wmi/

// Install package System.Management (6.0.0)
// using System.Management;

//create a management scope object
ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2");

//create object query
ObjectQuery query =
new ObjectQuery("SELECT * FROM Win32_VideoController "
+ "Where DeviceID=\"VideoController1\"");

//create object searcher
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, query);

//get a collection of WMI objects
ManagementObjectCollection queryCollection =
searcher.Get();

//enumerate the collection.
foreach (ManagementObject m in queryCollection)
{
// access properties of the WMI object
Console.WriteLine("CurrentHorizontalResolution : {0}",
m["CurrentHorizontalResolution"]);
Console.WriteLine("CurrentVerticalResolution : {0}",
m["CurrentVerticalResolution"]);
}


Related Topics



Leave a reply



Submit