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

相关推荐
神仙别闹1 小时前
基于 C# OpenPGP 的文件管理系统
开发语言·c#
海盗12343 小时前
C#在Distinct()中使用IEqualityComparer<T>
开发语言·c#
呼Lu噜6 小时前
基于C#的ASP.NET Core中分析async、await的使用场景
数据库·c#·asp.net
czhc11400756639 小时前
wpf 511 封装通信类 半导体协议:SECS
wpf
等故意9 小时前
C# 工业视觉上位机开发心得分享
开发语言·数码相机·c#·视觉检测
lingxiao168889 小时前
WPF数据采集和监控(Industrial)
wpf
时光追逐者9 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 70 期(2026年5.01-5.10)
c#·.net·.netcore
莫生灬灬9 小时前
ElementUI封装 共91个组件 支持易语言/火山/C#/Python
开发语言·c++·python·ui·elementui·c#
雨浓YN10 小时前
GKTGD 工业监控系统-02MySQL 数据库技术文档(类库:NET8_SQLData)
数据库·wpf