forked from ReClassNET/ReClass.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDpiUtil.cs
More file actions
159 lines (126 loc) · 2.93 KB
/
DpiUtil.cs
File metadata and controls
159 lines (126 loc) · 2.93 KB
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
using System;
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using ReClassNET.Native;
namespace ReClassNET.UI
{
public static class DpiUtil
{
private const int StdDpi = 96;
private static bool initialized;
private static int dpiX = StdDpi;
private static int dpiY = StdDpi;
private static double scaleX = 1.0;
private static double scaleY = 1.0;
public static bool ScalingRequired
{
get
{
if (Program.DesignMode)
{
return false;
}
EnsureInitialized();
return dpiX != StdDpi || dpiY != StdDpi;
}
}
private static void EnsureInitialized()
{
if (initialized)
{
return;
}
try
{
using (var g = Graphics.FromHwnd(IntPtr.Zero))
{
dpiX = (int)g.DpiX;
dpiY = (int)g.DpiY;
if (dpiX <= 0 || dpiY <= 0)
{
dpiX = StdDpi;
dpiY = StdDpi;
}
}
}
catch
{
}
scaleX = dpiX / (double)StdDpi;
scaleY = dpiY / (double)StdDpi;
initialized = true;
}
public static void ConfigureProcess()
{
NativeMethods.SetProcessDpiAwareness();
}
public static int ScaleIntX(int i)
{
EnsureInitialized();
return (int)Math.Round(i * scaleX);
}
public static int ScaleIntY(int i)
{
EnsureInitialized();
return (int)Math.Round(i * scaleY);
}
public static Image ScaleImage(Image img)
{
if (img == null)
{
return null;
}
int w = img.Width;
int h = img.Height;
int sw = ScaleIntX(w);
int sh = ScaleIntY(h);
if (w == sw && h == sh)
{
return img;
}
return ScaleImage(img, sw, sh);
}
private static Image ScaleImage(Image img, int w, int h)
{
Contract.Requires(img != null);
Contract.Requires(w >= 0);
Contract.Requires(h >= 0);
var bmp = new Bitmap(w, h, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.Transparent);
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
var wSrc = img.Width;
var hSrc = img.Height;
InterpolationMode im = InterpolationMode.HighQualityBicubic;
if (wSrc > 0 && hSrc > 0)
{
if ((w % wSrc) == 0 && (h % hSrc) == 0)
{
im = InterpolationMode.NearestNeighbor;
}
}
g.InterpolationMode = im;
var rSource = new RectangleF(0.0f, 0.0f, wSrc, hSrc);
var rDest = new RectangleF(0.0f, 0.0f, w, h);
AdjustScaleRects(ref rSource, ref rDest);
g.DrawImage(img, rDest, rSource, GraphicsUnit.Pixel);
}
return bmp;
}
private static void AdjustScaleRects(ref RectangleF rSource, ref RectangleF rDest)
{
if (rDest.Width > rSource.Width)
rSource.X = rSource.X - 0.5f;
if (rDest.Height > rSource.Height)
rSource.Y = rSource.Y - 0.5f;
if (rDest.Width < rSource.Width)
rSource.X = rSource.X + 0.5f;
if (rDest.Height < rSource.Height)
rSource.Y = rSource.Y + 0.5f;
}
}
}