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

相关推荐
ez52fF0k83 分钟前
.NET11云原生CI/CD在云原生应用持续集成与交付安全加固
前端·c#·交互
没什么本事12 小时前
关于C# panel 添加lable问题 -- 明确X和Y 位置错误
android·java·c#
火星papa13 小时前
C# 实现平滑流畅的进度条ProgressBar
c#·进度条·progressbar·平滑流畅
游乐码15 小时前
UnityGUI(五)GUI控件综合使用
开发语言·unity·c#
程序leo源15 小时前
C语言知识总结
c语言·开发语言·c++·经验分享·笔记·青少年编程·c#
烛阴16 小时前
TEngine 入门系列(二):三件套环境搭建 -- Unity + TEngine + AI 助手
前端·c#·unity3d
The Shio20 小时前
OptiByte 操练场:面向 IoT/嵌入式的协议可视化调试工具
网络·嵌入式硬件·物联网·c#·.net·业界资讯·iot
龙侠九重天1 天前
C# 调用 TensorFlow:迁移学习与模型推理实战指南
人工智能·深度学习·机器学习·c#·tensorflow·迁移学习·tensorflow.net
我是唐青枫1 天前
C#.NET YARP 认证授权实战:在网关层统一接入 JWT
开发语言·c#·.net
暖馒1 天前
WPF绑定由简到繁深入笔记
笔记·wpf