【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#索引器
开发语言·c#
Never_Satisfied2 小时前
在c#中,实现把图片文件拖动到pictureBox控件上
开发语言·c#
游乐码3 小时前
c#静态成员
开发语言·c#
qq_454245034 小时前
Graphkey:使用占位符彻底解耦函数与工作流
数据结构·c#
26岁的学习随笔5 小时前
【Claude Code】我给 Claude Code 做了个桌面启动器 —— 内置道家呼吸引导的悬浮路径工具
c#·开源项目·winforms·桌面工具·claude code
专注VB编程开发20年5 小时前
c#,vb.net Redis vs ODBC/ADO 查库的速度差距,写入json数据和字典数据
redis·c#·.net
zls3653655 小时前
C# WPF canvas中绘制缺陷分布map
开发语言·c#·wpf
专注VB编程开发20年5 小时前
c#Redis扣款锁的设计,多用户,多台电脑操作
wpf
qq_454245035 小时前
上下文驱动的 ECS:一种反应式实体组件系统扩展
数据结构·算法·c#
大鹏说大话6 小时前
基于.NET和C#构建光伏IoT物模型方案:架构设计与实践指南
物联网·c#·.net