Csharp 如何获取显示的分辨率
时间: 2023-06-02 22:07:13 浏览: 60
您好,我是ChitGPT,很高兴回答您的问题。要获取屏幕分辨率,可以使用C#中的Screen类,具体方法如下:
1. 引入命名空间
using System.Windows.Forms;
2. 获取屏幕分辨率
int screenWidth = Screen.PrimaryScreen.Bounds.Width;
int screenHeight = Screen.PrimaryScreen.Bounds.Height;
注意:这里获取的是主屏幕的分辨率,如果有多个显示器,则需要根据需要获取相应的屏幕。如果需要获取当前窗口的分辨率,可以使用Form的Bounds属性。另外,还可以使用WMI获取屏幕分辨率,不过比较麻烦,这里就不再赘述了。
相关问题
wpf 获取当前分辨率
在 WPF 中,你可以使用 System.Windows.SystemParameters 类来获取当前屏幕的分辨率。以下是获取当前屏幕分辨率的示例代码:
```csharp
double screenWidth = SystemParameters.PrimaryScreenWidth;
double screenHeight = SystemParameters.PrimaryScreenHeight;
Console.WriteLine("当前屏幕分辨率为:" + screenWidth + " x " + screenHeight);
```
请注意,此代码需要引用 System.Windows 命名空间。另外,需要注意的是,WPF 应用程序可能会缩放以适应高 DPI 显示屏,因此获取的分辨率可能与实际分辨率不同。如果需要考虑 DPI 缩放,请使用 System.Windows.SystemParameters.WorkArea 属性。
winform电脑分辨率
WinForm 是一个用于构建 Windows 桌面应用程序的.NET 框架,而电脑分辨率是指显示屏的像素数量。在 WinForm 应用程序中,你可以通过以下代码获取当前电脑的分辨率信息:
```csharp
int screenWidth = Screen.PrimaryScreen.Bounds.Width;
int screenHeight = Screen.PrimaryScreen.Bounds.Height;
```
这段代码使用了 Screen 类的 PrimaryScreen 属性来获取主屏幕的信息,并通过 Bounds 属性获取其宽度和高度。注意,你需要在代码文件中引入 `System.Windows.Forms` 命名空间。
请注意,如果你有多个显示屏,你可能需要进一步处理来获取特定显示屏的分辨率信息。