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

相关推荐
2501_930707781 天前
使用C#代码更改 PowerPoint 幻灯片大小
开发语言·c#·powerpoint
Z_W_H_1 天前
【C#】C#中值类型和引用类型参数传递的区别
开发语言·c#
用户8356290780511 天前
使用 C# 高效解析 PDF 文档:文本与表格提取实战指南
后端·c#
rit84324991 天前
C#实现的远程控制系统
前端·javascript·c#
wuguan_1 天前
C#中的静态成员、常量和只读变量
开发语言·c#
秋雨雁南飞1 天前
C# SignalR 添加Swagger
c#·swagger·signalr
张人玉1 天前
C# 与西门子 PLC 通信:地址相关核心知识点
开发语言·microsoft·c#·plc
Yuuuuuuu1 天前
WPF基于Canvas绘制多边形ROI
c#
缺点内向1 天前
如何在 C# 中重命名 Excel 工作表并设置标签颜色
开发语言·c#·excel
a努力。1 天前
网易Java面试被问:偏向锁在什么场景下反而降低性能?如何关闭?
java·开发语言·后端·面试·架构·c#