【C#】WPF获取屏幕分辨率

SystemParameters提供的接口,其实是获取渲染过程中的实际高宽,是受系统DPI设置的影响。

以 1920 * 1080 和 125% DPI为例:

分辨率高度:1080,实际获取的高度为:864。 分辨率宽度:1920,实际获取的宽度为:1536。

结果展示

代码

需要额外的包 using System.Drawing;

cs 复制代码
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp16
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            double currentGraphics = Graphics.FromHwnd(new WindowInteropHelper(Application.Current.MainWindow).Handle).DpiX / 96;
            string screenHeight = (SystemParameters.PrimaryScreenHeight* currentGraphics).ToString();
            string screenWidth = (SystemParameters.PrimaryScreenWidth * currentGraphics).ToString();
            InitializeComponent();
            textBlock.Text = screenWidth + "x" + screenHeight;
        }
    }
}

参考文献

WPF 如何正确获取屏幕分辨率 (huchengv5.github.io)

相关推荐
牛哇网络工作室7 小时前
UnityHDRP写实数字人全流程基础5—语音输入和语音识别
android·unity·c#·游戏引擎·aigc·语音识别·xcode
zlinear数据采集卡18 小时前
数据采集卡从入门到精通(9):分辨率与精度——16位卡不等于1/65536的精度
开发语言·数据库·fpga开发·开源·c#
深澈Vincel21 小时前
我用 C# + WebView2 做了一个针对“流氓软件”的“一键清理工具“:架构实践与踩坑记录
c#
zlinear数据采集卡21 小时前
数据采集卡从入门到精通(7):流水线型ADC——级级接力,高速与高精的平衡术
开发语言·arm开发·嵌入式硬件·fpga开发·c#
kingwebo'sZone1 天前
C# 根据公式计算的方法
开发语言·c#
阿部多瑞 ABU1 天前
告别手工核图:基于 .NET + MuPDFCore 的 CAD 等轴测 PDF 材料表提取与新旧版本对比实战
后端·算法·ui·pdf·c#
条形码D1 天前
DRG综合管理分析系统-目前已经支持CHS-DRG3.0
c#·drg·3.0分组器·drg分组器·chs-drg3.0分组器
Alex Gram2 天前
数据库同步工具PanguSync图文教程
java·mysql·postgresql·sqlserver·c#·数据库同步软件·数据库同步工具
头茬韭菜2 天前
第 10 篇:「Fluss 实战案例集」—— 完整解决方案与最佳实践
c#·linq·fluss
FuckPatience2 天前
WPF 给一个界面上的按钮应用同一种样式
wpf