schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
 
namespace XCommon.Image
{
    public struct Dpi
    {
        public Dpi(Double x, Double y)
        {
            DpiX = x;
            DpiY = y;
 
            Px2WpfX = 96 / DpiX;
            Px2WpfY = 96 / DpiY;
        }
 
        public Double DpiX { get; }
 
        public Double DpiY { get; }
 
        public Double Px2WpfX { get; }
 
        public Double Px2WpfY { get; }
 
        /// <summary>
        /// 英寸-厘米
        /// </summary>
        public static readonly Double In2Cm = 2.54;
        /// <summary>
        /// 英寸-磅
        /// </summary>
        public static readonly Double In2Pt = 72;
        /// <summary>
        /// 厘米-wpf
        /// </summary>
        public static readonly Double Cm2Wpf = 96 / 2.54;
    }
 
    public sealed class DpiHelper
    {
        #region Graphics
 
        public static Dpi GetDpiByGraphics(IntPtr hWnd)
        {
            using (var graphics = Graphics.FromHwnd(hWnd))
            {
                return new Dpi(graphics.DpiX, graphics.DpiY);
            }
        }
 
        #endregion
 
        #region CompositionTarget
 
        public static Dpi GetDpiFromVisual(Visual visual)
        {
            var source = PresentationSource.FromVisual(visual);
            //return (source == null || source.CompositionTarget == null) ? GetDpiByWin32(IntPtr.Zero) : new Dpi(96.0 * source.CompositionTarget.TransformToDevice.M11 * 1.254, 96.0 * source.CompositionTarget.TransformToDevice.M22 * 1.254);
            return (source == null || source.CompositionTarget == null) ? GetDpiByWin32(IntPtr.Zero) : new Dpi(96.0 * source.CompositionTarget.TransformToDevice.M11, 96.0 * source.CompositionTarget.TransformToDevice.M22);
        }
 
        #endregion
 
        #region Win32 API
 
        private const Int32 LOGPIXELSX = 88;
        private const Int32 LOGPIXELSY = 90;
 
        [DllImport("gdi32.dll")]
        private static extern Int32 GetDeviceCaps(IntPtr hdc, Int32 index);
 
        [DllImport("user32.dll")]
        private static extern IntPtr GetDC(IntPtr hWnd);
 
        [DllImport("user32.dll")]
        private static extern Int32 ReleaseDC(IntPtr hWnd, IntPtr hDc);
 
        public static Dpi GetDpiByWin32(IntPtr hwnd)
        {
            var hDc = GetDC(hwnd);
 
            var dpiX = GetDeviceCaps(hDc, LOGPIXELSX);
            var dpiY = GetDeviceCaps(hDc, LOGPIXELSY);
 
            ReleaseDC(hwnd, hDc);
            return new Dpi(dpiX, dpiY);
        }
 
        #endregion
    }
}