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

相关推荐
yue0082 分钟前
C# winform窗体换皮肤
c#·image类
曹牧6 小时前
C#:无法从方法组转换为objec
开发语言·c#
刘欣的博客8 小时前
C# 从API接口获取对象而不用先定义对象类
c#·json动态创建对象
Charles_go9 小时前
C#中级、double和decimal有什么区别
开发语言·c#
5***a97510 小时前
后端配置中心选型,Nacos与Apollo
wpf
·心猿意码·10 小时前
WPF转换器机制
wpf
她说彩礼65万13 小时前
WPF命令
wpf
小熊熊知识库16 小时前
C#接入AI操作步骤详解(deepseek接入)
人工智能·flask·c#
玖笙&16 小时前
✨WPF编程进阶【7.3】集成动画(附源码)
c++·c#·wpf·visual studio
yue00818 小时前
C# 窗体渐变色
开发语言·javascript·c#