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

相关推荐
默 语5 小时前
HarmonyOS7基础属性全解(八):隐私遮罩 obscured——屏幕上看得清清楚楚,录屏里只剩占位图
wpf·harmonyos
Warson_L8 小时前
.NET 8 速成笔记 16:API 框架到底怎么用,谁是自带的、谁是自己写的、配置和测试库如何关联
c#·.net
淡海水12 小时前
01-10-认知篇-对比-方案选型矩阵
矩阵·c#·excel·游戏开发·luban
默 语13 小时前
HarmonyOS7基础属性全解(四):显隐控制 visibility——藏起来、占着位、干脆不存在,是三回事
wpf
reasonsummer14 小时前
【办公类-178-01】20261204医保需要“幼儿园园通学籍证明截图”(Python页面截图+转PDF)
开发语言·c#
yuzhiboyouye15 小时前
我做web前端八年多了,转C#怎么样,而且我现在的java是初级水平,问的通义
c#
yuzhiboyouye16 小时前
我现在初级java,中高级web前端。那我C#的学习路径是?问的tare chat智能体
c#
陈仓主教16 小时前
WPF 菜单(Menu)控件详解与使用—WPF 中的控件(23)--菜单(Menu)
wpf
看浪的路人1 天前
第10讲:运维 Agent——从「被动救火」到「自动驾驶」
c#·linq
Jazz_z2 天前
C# 实现 Word 文档内容复制
开发语言·c#·word