获取并设置屏幕分辨率

Bru*_*III 62 c# screen screen-resolution

如何使用Visual C#收集和更改屏幕分辨率?

Don*_*nut 84

检索屏幕分辨率,您将要使用System.Windows.Forms.Screen该类.该Screen.AllScreens属性可用于访问系统上所有显示的集合,或者您可以使用该Screen.PrimaryScreen属性访问主显示.

Screen类有一个叫做属性Bounds,你可以用它来确定类的当前实例的分辨率.例如,要确定当前屏幕的分辨率:

Rectangle resolution = Screen.PrimaryScreen.Bounds;
Run Code Online (Sandbox Code Playgroud)

为了改变分辨率,事情会变得复杂一些.本文(或本文)提供了详细的实现和说明.希望这可以帮助.

  • 有没有非 winforms 的方法来做到这一点?IE,可以与 .net core 一起使用的东西吗? (2认同)

小智 9

在C#中,这是如何获得分辨率屏幕:

按钮单击或表单加载:

string screenWidth = Screen.PrimaryScreen.Bounds.Width.ToString();
string screenHeight = Screen.PrimaryScreen.Bounds.Height.ToString();
Label1.Text = ("Resolution: " + screenWidth + "x" + screenHeight);
Run Code Online (Sandbox Code Playgroud)


小智 5

该代码将在WPF中完美运行。您可以在页面加载或按钮单击中使用它。

      string screenWidth =System.Windows.SystemParameters.PrimaryScreenWidth.ToString();

      string screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight.ToString();

      txtResolution.Text ="Resolution : "+screenWidth + " X " + screenHeight;
Run Code Online (Sandbox Code Playgroud)


小智 5

从不同的解决方案中得到答案以获得显示分辨率

  1. 获取缩放因子

  2. 通过缩放因子结果获取 Screen.PrimaryScreen.Bounds.Width 和 Screen.PrimaryScreen.Bounds.Height 倍数

    #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
    
    Run Code Online (Sandbox Code Playgroud)

检查显示分辨率

var size = GetDisplayResolution();
Console.WriteLine("Display Resoluton: " + size.Width + "x" + size.Height);
Run Code Online (Sandbox Code Playgroud)


Kei*_*thS 1

在 Winforms 中,有一个 Screen 类,您可以使用它来获取有关连接到计算机的所有显示器的屏幕尺寸和颜色深度的数据。这是文档页面:http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.aspx

更改屏幕分辨率比较棘手。有一个解析第三方类,它包装了您原本要挂钩的本机代码。使用其CResolution嵌套类将屏幕分辨率设置为新的高度和宽度;但请注意,这样做仅适用于显示器实际支持的高度/宽度组合(800x600、1024x768 等,而不是 817x435)。