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

相关推荐
Charles_go10 小时前
41、C#什么是单例设计模式
java·设计模式·c#
夏霞10 小时前
c# ASP.NET Core SignalR 客户端与服务端自动重连配置指南
开发语言·c#·asp.net
Scout-leaf10 小时前
九成九新自用C#入门文档
c#
烛阴13 小时前
隐式vs显式:解密C#类型转换的底层逻辑
前端·c#
梦里不知身是客1113 小时前
kafka作为Sink
c#·linq
猿来是你_L14 小时前
C# Dictionary 转换成 List
windows·c#·list
kokunka14 小时前
C#类修饰符功能与范围详解
java·开发语言·c#
mudtools15 小时前
.NET驾驭Excel之力:工作簿与工作表操作基础
c#·.net·excel
mudtools15 小时前
.NET驾驭Excel之力:单元格与区域操作详解
c#·.net·excel
用户83562907805118 小时前
C# 自动化生成 PowerPoint 演示文稿
后端·c#