【问题标题】:Get and Set Screen Resolution获取和设置屏幕分辨率
【发布时间】:2011-02-22 19:01:21
【问题描述】:

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

【问题讨论】:

  • 你见过this codeproject article吗?
  • @Justin:添加 SystemInformation.PrimaryMonitorSize 并将其作为答案发布。

标签: c# screen screen-resolution


【解决方案1】:

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

Screen 类有一个名为Bounds 的属性,您可以使用它来确定类的当前实例的分辨率。比如判断当前屏幕的分辨率:

Rectangle resolution = Screen.PrimaryScreen.Bounds;

对于更改分辨率,事情变得有点复杂。 This article(或this one)提供了详细的实现和解释。希望这会有所帮助。

【讨论】:

  • 有没有非winforms的方式来做到这一点? IE,适用于 .net 核心的东西?
  • 我需要桌面屏幕捕获的屏幕分辨率。 Zoomin 后它不起作用,它返回 Zoomed 分辨率,因此图片也缩放了 Zoomin Window 中实际屏幕的任何方法。
【解决方案2】:

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

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

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

      txtResolution.Text ="Resolution : "+screenWidth + " X " + screenHeight;

【讨论】:

    【解决方案3】:

    在 C# 中,这是获取屏幕分辨率的方法:

    按钮点击或表单加载:

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案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
          

        检查显示分辨率

        var size = GetDisplayResolution();
        Console.WriteLine("Display Resoluton: " + size.Width + "x" + size.Height);
        

        【讨论】:

        • 如何获取非主屏的缩放比例?
        • 这太棒了!但是,我可以提出一些建议吗?您正在使用 GetDeviceCaps 来获取 PhysicalScreenHeight,然后执行相同的操作来获取 LogicalScreenHeight,然后除以得到一个比率,然后通过乘以 Screen.PrimaryScreen.Bounds 来跟踪它......但你最终得到的都是是您已经询问过 gdi32.dll 的 PhysicalScreenHeight。难道你不能只调用两次,一次用于 PhysicalScreenHeight,另一次用于 PhysicalScreenWidth,然后就结束了吗?
        【解决方案6】:

        如果您想收集屏幕分辨率,您可以在 WPF 窗口中运行以下代码(该窗口是 this 所指的窗口):

        System.Windows.Media.Matrix m = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice;
        Double dpiX = m.M11 * 96;
        Double dpiY = m.M22 * 96;
        

        【讨论】:

        • 问题状态:“如何使用 Visual C# 收集和更改屏幕分辨率?”
        【解决方案7】:

        我花了很多时间试图弄清楚这一点,因为具有缩放因子的设备很难获得屏幕的实际尺寸,因为当调用C# 对象的返回值现在不正确,因此您需要进行 Windows API 调用以获取缩放因子并应用乘数,我为非 WPF 应用程序找到的唯一工作方式是这里

        https://stackoverflow.com/a/21450169

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多