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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace XCommon.Image
{
    /// <summary>
    /// 像素与物理长度的计算操作类
    /// </summary>
    public class PixelsCalculationHelper
    {
        #region Win32 API
        [DllImport("user32.dll")]
        static extern IntPtr GetDC(IntPtr ptr);
        [DllImport("gdi32.dll")]
        static extern int GetDeviceCaps(
        IntPtr hdc, // handle to DC
        int nIndex // index of capability
        );
        [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
        static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
        #endregion
 
        #region DeviceCaps常量
        const int HORZRES = 8;//物理屏幕宽度mm
        const int VERTRES = 10;//物理屏幕高度mm
        const int LOGPIXELSX = 88;//逻辑像素x
        const int LOGPIXELSY = 90;//逻辑像素y
        const int DESKTOPVERTRES = 117;//桌面高度
        const int DESKTOPHORZRES = 118;//桌面宽度
        #endregion
 
        #region 属性
        /// <summary>
        /// 宽度转化 length是毫米,1厘米=10毫米
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static double MillimetersToPixelsWidth(double length)
        {
            System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(IntPtr.Zero);
            IntPtr hdc = g.GetHdc();
            int width = GetDeviceCaps(hdc, 4); // HORZRES
            int pixels = GetDeviceCaps(hdc, 8); // BITSPIXEL
            double fg = pixels / width;
            g.ReleaseHdc(hdc);
            return (((double)pixels / (double)width) * (double)length);
        }
 
        /// <summary>
        /// 高度转化 length是毫米,1厘米=10毫米
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static double MillimetersToPixelsHeight(double length)
        {
            System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(IntPtr.Zero);
            IntPtr hdc = g.GetHdc();
            int height = GetDeviceCaps(hdc, 6); // HORZRES
            int pixels = GetDeviceCaps(hdc, 10); // BITSPIXEL
            double fg = pixels / height;
            g.ReleaseHdc(hdc);
            return (((double)pixels / (double)height) * (double)length);
        }
 
        /// <summary>
        /// 获取屏幕分辨率当前物理大小
        /// </summary>
        public static Size WorkingArea
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                Size size = new Size();
                size.Width = GetDeviceCaps(hdc, HORZRES);
                size.Height = GetDeviceCaps(hdc, VERTRES);
                ReleaseDC(IntPtr.Zero, hdc);
                return size;
            }
        }
        /// <summary>
        /// 当前系统DPI_X 大小 一般为96
        /// </summary>
        public static int DpiX
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                int DpiX = GetDeviceCaps(hdc, LOGPIXELSX);
                ReleaseDC(IntPtr.Zero, hdc);
                return DpiX;
            }
        }
        /// <summary>
        /// 当前系统DPI_Y 大小 一般为96
        /// </summary>
        public static int DpiY
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                int DpiX = GetDeviceCaps(hdc, LOGPIXELSY);
                ReleaseDC(IntPtr.Zero, hdc);
                return DpiX;
            }
        }
        /// <summary>
        /// 获取真实设置的桌面分辨率大小
        /// </summary>
        public static Size DESKTOP
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                Size size = new Size();
                size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES);
                size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES);
                ReleaseDC(IntPtr.Zero, hdc);
                return size;
            }
        }
 
        /// <summary>
        /// 获取宽度缩放百分比
        /// </summary>
        public static float ScaleX
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                int t = GetDeviceCaps(hdc, DESKTOPHORZRES);
                int d = GetDeviceCaps(hdc, HORZRES);
                float ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES);
                ReleaseDC(IntPtr.Zero, hdc);
                return ScaleX;
            }
        }
 
        /// <summary>
        /// 获取高度缩放百分比
        /// </summary>
        public static float ScaleY
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                float ScaleY = (float)(float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES);
                ReleaseDC(IntPtr.Zero, hdc);
                return ScaleY;
            }
        }
        #endregion
 
        #region 1个像素代表的实际长度
        /// <summary>
        /// 1个像素代表的实际长度
        /// </summary>
        /// <param name="pixels">像素大小</param>
        /// <param name="dpi">每英寸点数</param>
        /// <returns></returns>
        public static double PixelsToInches(int pixels, int dpi)
        {
            return (double)(pixels) / dpi * 25.4;
        }
        #endregion
    }
}