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

相关推荐
大王小生13 小时前
说说CSV文件和C#解析csv文件的几种方式
人工智能·c#·csv·csvhelper·csvreader
LongtengGensSupreme14 小时前
C# 中监听 IPv6 回环地址----HttpListener
c#·ipv6·httplistener
zzcufo16 小时前
多邻国学习笔记第五阶段第10-11部分
笔记·学习·c#
easyboot1 天前
C#使用pythonnet简单示例
开发语言·python·c#
刘欣的博客1 天前
c# winform 控件dock 停造位置、摆放顺序问题
c#·winform·dock停靠问题
Java程序员威哥1 天前
Arthas+IDEA实战:Java线上问题排查完整流程(Spring Boot项目落地)
java·开发语言·spring boot·python·c#·intellij-idea
easyboot1 天前
C#通过sqlsugar插入数据到postgresql
开发语言·c#
阿蒙Amon1 天前
C#每日面试题-break、continue和goto的区别
java·面试·c#
小北方城市网1 天前
Spring Cloud Gateway 生产级实践:高可用架构、灰度发布与故障排查
spring boot·redis·分布式·缓存·架构·wpf
mudtools1 天前
C#中基于Word COM组件的数学公式排版实践
开发语言·c#·word