C#: Get Complete Desktop Size

C#: Get complete desktop size?

You have two options:

  1. PresentationFramework.dll

    SystemParameters.VirtualScreenWidth   
    SystemParameters.VirtualScreenHeight
  2. System.Windows.Forms.dll

    SystemInformation.VirtualScreen.Width   
    SystemInformation.VirtualScreen.Height

Use the first option if you developing a WPF application.

How can I get the active screen dimensions?

Screen.FromControl, Screen.FromPoint and Screen.FromRectangle should help you with this. For example in WinForms it would be:

class MyForm : Form
{
public Rectangle GetScreen()
{
return Screen.FromControl(this).Bounds;
}
}

I don't know of an equivalent call for WPF. Therefore, you need to do something like this extension method.

static class ExtensionsForWPF
{
public static System.Windows.Forms.Screen GetScreen(this Window window)
{
return System.Windows.Forms.Screen.FromHandle(new WindowInteropHelper(window).Handle);
}
}

Get Desktop Size from Windows Service?

You cannot do this inside the service. Services run in session 0, where no GDI functions work. Once the process is created, you cannot change sessions, and cannot use UI in different session.

One of the possible solutions for you is to launch a new process in user session. You can start looking at this SO question. The other pre-requisites for this method is that your service has to be running as Local System, so that you can enable SE_TCB_NAME privilege (by calling AdjustTokenPrivilegies). Since you are saying you already hooked up to the user logon notification, you should be able to extract session ID of the session that you are interested in.

Once you have a process lauched in user session, you have to pass the result from the new process back to your service process. For that any kind of IPC mechanism could be used.

How to get the screen resolution without Windows Forms reference?

If you don't want to use System.Windows.Forms (or cannot), you can get the screen resolution by using a Windows API function EnumDisplaySettings.

To call the WinAPI function, you can use the P/Invoke feature that is also available on .NET Core. Please note that this will only work on a Windows system, because there's no WinAPI on non-Windows targets.

The function declaration looks as follows:

[DllImport("user32.dll")]
static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);

You also need the WinAPI DEVMODE struct definition:

[StructLayout(LayoutKind.Sequential)]
struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public int dmPositionX;
public int dmPositionY;
public int dmDisplayOrientation;
public int dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
}

Actually, you don't need most of this structure's fields. The interesting ones are dmPelsWidth and dmPelsHeight.

Call the function like this:

const int ENUM_CURRENT_SETTINGS = -1;

DEVMODE devMode = default;
devMode.dmSize = (short)Marshal.SizeOf(devMode);
EnumDisplaySettings(null, ENUM_CURRENT_SETTINGS, ref devMode);

Now you can check the screen resolution in the dmPelsWidth and dmPelsHeight fields of the devMode struct.

Since we specify null as first argument, the function describes the current display device on the computer on which the calling thread is running.

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

C# Screen size without references in interactive

Here's an example I came up with.

I have noticed that it does not work correctly on High-DPI Screens. It will report the apparent resolution, not the actual resolution.

    static void Main(string[] args)
{
var size = GetScreenSize();
Console.WriteLine(size.Length + " x " + size.Width);
Console.ReadLine();
}

static Size GetScreenSize()
{
return new Size(GetSystemMetrics(0), GetSystemMetrics(1));
}

struct Size
{
public Size(int l, int w)
{
Length = l;
Width = w;
}

public int Length { get; set; }
public int Width { get; set; }
}

[DllImport("User32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern int GetSystemMetrics(int nIndex);

Get screen size in pixels in windows form in C#

Check the Screen class and its property Bounds

Screen.PrimaryScreen.Bounds.Width;
Screen.PrimaryScreen.Bounds.Height;
Screen.PrimaryScreen.Bounds.Size;

Determine screen size with .NET Core 5?

The comments hint that it is not supported yet.

So I show you what we have done to implement it using Windows Forms (took me some time to understand how to reference Windows Forms in a .NET Core library project correctly):

You can add the Windows Forms dependency like so to the csproj file:

<ItemGroup>
<FrameworkReference Include="Microsoft.WindowsDesktop.App.WindowsForms" />
</ItemGroup>

Then you can use the Screen class again:

new Size
{
Width = Screen.AllScreens.Sum(s => s.Bounds.Width),
Height = Screen.AllScreens.Max(s => s.Bounds.Height)
}

IMPORTANT: This works on Windows only, but at least it is .NET Core compatible.



Related Topics



Leave a reply



Submit