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

相关推荐
_oP_i2 小时前
WinForms 项目里生成时选择“首选目标平台 32 位导致有些电脑在获取office word对象时获取不到
c#·office
要记得喝水3 小时前
C#某公司面试题(含题目和解析)--1
开发语言·windows·面试·c#·.net
冷冷的菜哥4 小时前
ASP.NET Core文件分片上传
c#·asp.net·asp.net core·文件分片上传
上位机付工6 小时前
2025年了,学C#上位机需要什么条件
c#·上位机·西门子
c#上位机6 小时前
wpf之Border
c#·wpf
SunflowerCoder6 小时前
WPF迁移avalonia之图像处理(一)
图像处理·wpf·avalonia
周杰伦fans6 小时前
WPF中的DataContext以及常见的绑定方式
wpf
almighty2713 小时前
C# 阿里云 OSS 图片上传步骤及浏览器查看方法
c#·图片上传·c# 阿里云 oss·阿里云 oss