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

相关推荐
农村小镇哥1 天前
C#中的字符串格式化
服务器·开发语言·c#
czhc11400756631 天前
719:StartPoint;Margin;ResourceDictionar;Component.model;lds;
c#
贺国亚1 天前
模型训练-分布式与GPU调度
分布式·wpf
-银雾鸢尾-1 天前
C#中的万物之父——Object类
开发语言·c#
水晶石NSIS专栏1 天前
Geek_Studio - 安卓终端调试工作台
android·c#·apk签名·coloros主题
-银雾鸢尾-1 天前
C#中的继承
开发语言·c#
不羁的木木2 天前
HarmonyOS技术精讲-Connectivity Kit:实战——多屏协同与文件快传应用
华为·wpf·harmonyos
吴可可1232 天前
C#AutoCAD二次开发打断多段线
c#
-银雾鸢尾-2 天前
里氏替换原则
开发语言·c#·里氏替换原则