【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)

相关推荐
翼帆3 小时前
.NET 程序保护实战系列01-流水线架构与保护引擎总览
c#·破解
落寞的电源4 小时前
Delegate = Object + MethodInfo
开发语言·数据库·c#
Ricky_Theseus6 小时前
Trie 字典树:前缀匹配利器
开发语言·c#
北域码匠20 小时前
嵌入式限幅滤波:工业信号降噪利器
c#·传感器采集·数据预处理·嵌入式算法·限幅滤波·数字滤波·数据降噪
云中飞鸿1 天前
WPF分哪几块
wpf
csdn_aspnet1 天前
C# 提取、截取或匹配字符串内包含指定字符的一些方法分享
c#·字符串·正则·分割·提取·匹配
枳实-叶1 天前
【Linux驱动开发】第23天:spi_driver 的 probe / remove 函数实现规范
linux·驱动开发·c#
长明1 天前
C#项目组织与概念梳理
后端·c#
迷路爸爸1801 天前
Python collections 入门+实战
windows·python·c#·collections·dict
csdn_aspnet1 天前
C# 截取或匹配字符串内包含指定字符的一些方法
c#·字符串·分割·string·匹配·截取