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

相关推荐
钰fly1 小时前
C#文件与数据操作核心概念手册
c#
阿蒙Amon2 小时前
C#每日面试题-简述C#访问修饰符
windows·microsoft·c#
Kiyra2 小时前
WebSocket vs HTTP:为什么 IM 系统选择长连接?
分布式·websocket·网络协议·http·设计模式·系统架构·wpf
酩酊仙人4 小时前
ABP+Hangfire实现定时任务
后端·c#·asp.net·hangfire
阿蒙Amon5 小时前
C#每日面试题-属性和特性的区别
java·面试·c#
要记得喝水5 小时前
某公司C#-WPF面试题-来自nowcoder(含答案和解析)--2
c#·wpf
Joker 0076 小时前
Linux nohup命令实战指南
linux·运维·wpf
爱敲点代码的小哥9 小时前
类型转换 递归算法 编译错误 装箱和拆箱 知识点
开发语言·c#
时光追逐者10 小时前
一个 WPF 开源、免费的 SVG 图像查看控件
开源·c#·.net·wpf
江沉晚呤时10 小时前
构建智能代理的利器:深入解析 Microsoft Agent Framework
开发语言·c#