如何在WPF中获取DPI?
答案 0 :(得分:68)
http://blogs.msdn.com/jaimer/archive/2007/03/07/getting-system-dpi-in-wpf-app.aspx似乎有效
PresentationSource source = PresentationSource.FromVisual(this);
double dpiX, dpiY;
if (source != null) {
dpiX = 96.0 * source.CompositionTarget.TransformToDevice.M11;
dpiY = 96.0 * source.CompositionTarget.TransformToDevice.M22;
}
答案 1 :(得分:45)
var dpiXProperty = typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static);
var dpiYProperty = typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static);
var dpiX = (int)dpiXProperty.GetValue(null, null);
var dpiY = (int)dpiYProperty.GetValue(null, null);
答案 2 :(得分:15)
使用.NET 4.6.2 Preview及更高版本,您可以调用VisualTreeHelper.GetDpi(Visual visual)
。它返回一个DpiScale
结构,告诉你将要呈现或已经呈现给定Visual
的DPI。
答案 3 :(得分:5)
我找到的唯一方法是获得真实的"监视器dpi如下。所有其他提到的技术只是说96对大多数监视器都不正确。
public class ScreenInformations
{
public static uint RawDpi { get; private set; }
static ScreenInformations()
{
uint dpiX;
uint dpiY;
GetDpi(DpiType.RAW, out dpiX, out dpiY);
RawDpi = dpiX;
}
/// <summary>
/// Returns the scaling of the given screen.
/// </summary>
/// <param name="dpiType">The type of dpi that should be given back..</param>
/// <param name="dpiX">Gives the horizontal scaling back (in dpi).</param>
/// <param name="dpiY">Gives the vertical scaling back (in dpi).</param>
private static void GetDpi(DpiType dpiType, out uint dpiX, out uint dpiY)
{
var point = new System.Drawing.Point(1, 1);
var hmonitor = MonitorFromPoint(point, _MONITOR_DEFAULTTONEAREST);
switch (GetDpiForMonitor(hmonitor, dpiType, out dpiX, out dpiY).ToInt32())
{
case _S_OK: return;
case _E_INVALIDARG:
throw new ArgumentException("Unknown error. See https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510.aspx for more information.");
default:
throw new COMException("Unknown error. See https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510.aspx for more information.");
}
}
//https://msdn.microsoft.com/en-us/library/windows/desktop/dd145062.aspx
[DllImport("User32.dll")]
private static extern IntPtr MonitorFromPoint([In]System.Drawing.Point pt, [In]uint dwFlags);
//https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510.aspx
[DllImport("Shcore.dll")]
private static extern IntPtr GetDpiForMonitor([In]IntPtr hmonitor, [In]DpiType dpiType, [Out]out uint dpiX, [Out]out uint dpiY);
const int _S_OK = 0;
const int _MONITOR_DEFAULTTONEAREST = 2;
const int _E_INVALIDARG = -2147024809;
}
/// <summary>
/// Represents the different types of scaling.
/// </summary>
/// <seealso cref="https://msdn.microsoft.com/en-us/library/windows/desktop/dn280511.aspx"/>
public enum DpiType
{
EFFECTIVE = 0,
ANGULAR = 1,
RAW = 2,
}
答案 4 :(得分:3)
这是一种依赖于Direct2D技术的方法(在Windows Vista SP2和更高版本以及服务器上支持),因此它在WPF中工作正常(基于相同的理由)。它使用ID2D1Factory::GetDesktopDpi method
public static class DpiUtilities
{
private static Lazy<Tuple<float, float>> _dpi = new Lazy<Tuple<float, float>>(ReadDpi);
public static float DesktopDpiX
{
get
{
return _dpi.Value.Item1;
}
}
public static float DesktopDpiY
{
get
{
return _dpi.Value.Item2;
}
}
public static void Reload()
{
ID2D1Factory factory;
int hr = D2D1CreateFactory(D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED, typeof(ID2D1Factory).GUID, IntPtr.Zero, out factory);
if (hr != 0)
Marshal.ThrowExceptionForHR(hr);
factory.ReloadSystemMetrics();
Marshal.ReleaseComObject(factory);
}
private static Tuple<float, float> ReadDpi()
{
ID2D1Factory factory;
int hr = D2D1CreateFactory(D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED, typeof(ID2D1Factory).GUID, IntPtr.Zero, out factory);
if (hr != 0)
Marshal.ThrowExceptionForHR(hr);
float x;
float y;
factory.GetDesktopDpi(out x, out y);
Marshal.ReleaseComObject(factory);
return new Tuple<float, float>(x, y);
}
[DllImport("d2d1.dll")]
private static extern int D2D1CreateFactory(D2D1_FACTORY_TYPE factoryType, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, IntPtr pFactoryOptions, out ID2D1Factory ppIFactory);
private enum D2D1_FACTORY_TYPE
{
D2D1_FACTORY_TYPE_SINGLE_THREADED = 0,
D2D1_FACTORY_TYPE_MULTI_THREADED = 1,
}
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("06152247-6f50-465a-9245-118bfd3b6007")]
private interface ID2D1Factory
{
int ReloadSystemMetrics();
[PreserveSig]
void GetDesktopDpi(out float dpiX, out float dpiY);
// the rest is not implemented as we don't need it
}
}
答案 5 :(得分:1)
您可以尝试使用 ManagementClass:
public static string GetDPI()
{
using (ManagementClass mc = new ManagementClass("Win32_DesktopMonitor"))
{
using (ManagementObjectCollection moc = mc.GetInstances())
{
int PixelsPerXLogicalInch = 0; // dpi for x
int PixelsPerYLogicalInch = 0; // dpi for y
foreach (ManagementObject each in moc)
{
PixelsPerXLogicalInch = int.Parse((each.Properties["PixelsPerXLogicalInch"].Value.ToString()));
PixelsPerYLogicalInch = int.Parse((each.Properties["PixelsPerYLogicalInch"].Value.ToString()));
}
return PixelsPerXLogicalInch + "," + PixelsPerYLogicalInch;
}
}
}
答案 6 :(得分:0)
2017年1月25日下午3:54
“从桌面应用程序调用Windows 10 API” 和
https://docs.microsoft.com/en-us/uwp/api/windows.devices.display.displaymonitor
“显示器类”
命名空间:Windows.Devices.Display程序集:Windows.Devices.Display.dll,Windows.dll
提供有关连接到系统的显示监视器设备的信息。
这些数据包括显示器的扩展显示标识数据(EDID,这是一种工业标准的显示描述符块,几乎所有显示器都用来提供支持的模式和常规设备信息的描述)的常用信息,以及DisplayID(即最新的行业标准,提供了EDID的超集。
原始DpiX
获取显示器的物理水平DPI(基于显示器的原始分辨率和物理尺寸)。
原始DpiY
获取显示器的物理垂直DPI(基于显示器的原始分辨率和物理尺寸)。
答案 7 :(得分:0)
2006年以来Windows中的基本监视器信息
https://docs.microsoft.com/en-us/windows/desktop/wmicoreprov/msmonitorclass
MSMonitorClass类
WmiMonitorRawEEdidV1Block类
WmiMonitorBasicDisplayParams类
MaxHorizontalImageSize ( EDID byte 21 )
MaxVerticalImageSize ( EDID byte 22 )
(EDID的大小在EDID详细计时描述符中以厘米为单位,以毫米为单位
12个水平图像尺寸,毫米,8 lsbits(0–4095毫米,161英寸)
13垂直图像尺寸,毫米,8 lsbits(0–4095毫米,161英寸)
14位7–4水平图像大小,mm,4 msbits
位3–0垂直图像大小,毫米,4毫秒
)
和
答案 8 :(得分:0)
这就是我设法在WPF中获得“比例因子”的方法 我的笔记本电脑的分辨率为1920x1440。
$ awk '{printf "rename %s ", $1; gsub("lbsrv", "lb")}1' file