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

相关推荐
电商api接口开发10 小时前
ASP.NET MVC 入门指南
c#·asp.net·mvc
我不是程序猿儿11 小时前
[C#]反射的实战应用,实际数据模拟
开发语言·c#
爱编程的鱼11 小时前
C# 结构(Struct)
开发语言·人工智能·算法·c#
是阿根12 小时前
unity使用iTextSharp生成PDF文件
unity·c#·游戏引擎
ABAP 成13 小时前
.NET Framework 4.0可用EXCEL导入至DataTable
c#
WineMonk16 小时前
C#多线程访问资源
c#
Bardb16 小时前
04-stm32的标准外设库
stm32·c#
风,停下16 小时前
C#基于Sunnyui框架和MVC模式实现用户登录管理
设计模式·c#·mvc
钢铁男儿16 小时前
C# 实战_RichTextBox选中某一行条目高亮,离开恢复
开发语言·c#
千叶真尹19 小时前
【无标题】
c#·linq