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

相关推荐
qq_4312807014 小时前
上位与基恩士PLC通讯工作记录
c#
weixin_4280053018 小时前
C#调用 AI学习从0开始-第1阶段(基础与工具)-第2天Prompt工程基础
人工智能·学习·c#·prompt
咩图19 小时前
WPF-VisualStudio-C#-Fluent.Ribbon8.0.0学习
c#·wpf·visual studio
拼尽全力前进20 小时前
JDDL 核心实现原理与架构解析
架构·wpf
加号320 小时前
【C#】WPF基于Halcon 的HWindowControlWPF 控件实现图像缩放、移动
开发语言·c#·wpf
雪豹阿伟21 小时前
2.C# —— 结构体、类型转换与运算符
c#·上位机
njsgcs1 天前
c# solidworks GetPartBox无法获得正确实体边界框原因
开发语言·c#·solidworks
码农的神经元1 天前
考虑通信时延的直流微电网分布式电-氢混合储能协同控制仿真复现与改进
分布式·wpf
rockey6271 天前
AScript之匿名类型与动态类型
c#·.net·script·eval·expression·动态脚本
99乘法口诀万物皆可变1 天前
BMS HIL 自动化测试框架方案(基于 CANoe + C# + Excel)
开发语言·c#·excel