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

相关推荐
bugcome_com1 小时前
C# 中的封装与访问修饰符
c#
游乐码2 小时前
c#成员属性
开发语言·c#
Never_Satisfied3 小时前
在c#中,如何删除字符串中的第x个字符
开发语言·c#
闲人编程4 小时前
定时任务与周期性调度
分布式·python·wpf·调度·cron·定时人物·周期性
Never_Satisfied4 小时前
在c#中,缩放jpg文件的尺寸
算法·c#
Never_Satisfied4 小时前
在c#中,控件的事件执行耗时操作导致窗体无法及时处理绘制、鼠标点击
开发语言·c#
zls3653655 小时前
C# WPF canvas中绘制缺陷分布map并实现缩放
开发语言·c#·wpf
Never_Satisfied5 小时前
在c#中,如何在字符串的第x个字符位置插入字符
java·开发语言·c#
bugcome_com19 小时前
C# 常量详解:从基础语法到实际应用
c#
qq_1508419921 小时前
3天基于VS2026的C#编程入门及动态调用CH341DLLA64读写I2C从机
开发语言·c#