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

相关推荐
_Cherry|1 天前
Unity中窗口调整
unity·c#·游戏引擎
c#上位机1 天前
halcon刚性变换(平移+旋转)——vector_angle_to_rigid
人工智能·计算机视觉·c#·上位机·halcon·机器视觉
小码编匠1 天前
WPF 实现高仿 Windows 通知提示框:工业级弹窗设计与实现
后端·c#·.net
SEO-狼术1 天前
Telerik UI for WPF and WinForms 2025 Q4 Crack
ui·wpf
武藤一雄1 天前
[WPF] 万字拆解依赖属性与附加属性
前端·microsoft·c#·.net·wpf
Macbethad1 天前
工业设备配方管理程序技术方案
wpf
我好喜欢你~1 天前
.net---图表库(LiveCharts.Wpf)
c#·wpf
helloworddm1 天前
GrainType详解
服务器·网络·架构·c#
yngsqq1 天前
CAD点集生成泰森多边形Voronoi图)——CAD二次开发
c#
分布式存储与RustFS1 天前
RustFS:基于Rust的高性能分布式对象存储,重新定义数据存储新标准!
人工智能·wpf·对象存储·minio·高可用·企业存储·rustfs