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

相关推荐
cici1587425 分钟前
C# LAS 点云读取与处理工具
stm32·单片机·c#
OnlyEasyCode42 分钟前
C# 发送QQ邮箱验证码or其他
开发语言·c#
123的故事2 小时前
工具分享(2)-NSmartProxy内网穿透工具。
c#·.net·nsmartproxy
SunnyDays10113 小时前
使用 C# 添加、修改和删除 Excel VBA 宏 (无需 Microsoft Office Interop)
c#·excel··vba
影寂ldy4 小时前
C# 多接口、同名冲突、显式实现、接口继承 完整笔记
java·笔记·c#
诸葛大钢铁4 小时前
如何降低Word文件的体积?压缩Word文件的三种方法
开发语言·c#
专注VB编程开发20年4 小时前
阿里通义灵码插件安装失败
开发语言·ide·c#·visual studio
影寂ldy5 小时前
C# 泛型方法
java·前端·c#
caimouse5 小时前
Godot 4.7 内嵌 C# 模块切换到 .NET 9.0 编译指南
c#·.net·godot