-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathDpiUtil.cs
More file actions
134 lines (107 loc) · 2.95 KB
/
DpiUtil.cs
File metadata and controls
134 lines (107 loc) · 2.95 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
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
{
public const int DefalutDpi = 96;
private static int dpiX = DefalutDpi;
private static int dpiY = DefalutDpi;
private static double scaleX = 1.0;
private static double scaleY = 1.0;
public static void ConfigureProcess()
{
NativeMethods.SetProcessDpiAwareness();
}
public static void SetDpi(int x, int y)
{
dpiX = x;
dpiY = y;
if (dpiX <= 0 || dpiY <= 0)
{
dpiX = DefalutDpi;
dpiY = DefalutDpi;
}
scaleX = dpiX / (double)DefalutDpi;
scaleY = dpiY / (double)DefalutDpi;
}
public static void TrySetDpiFromCurrentDesktop()
{
try
{
using var g = Graphics.FromHwnd(IntPtr.Zero);
SetDpi((int)g.DpiX, (int)g.DpiY);
}
catch
{
// ignored
}
}
public static int ScaleIntX(int i)
{
return (int)Math.Round(i * scaleX);
}
public static int ScaleIntY(int i)
{
return (int)Math.Round(i * scaleY);
}
public static Image ScaleImage(Image sourceImage)
{
if (sourceImage == null)
{
return null;
}
var width = sourceImage.Width;
var height = sourceImage.Height;
var scaledWidth = ScaleIntX(width);
var scaledHeight = ScaleIntY(height);
if (width == scaledWidth && height == scaledHeight)
{
return sourceImage;
}
return ScaleImage(sourceImage, scaledWidth, scaledHeight);
}
private static Image ScaleImage(Image sourceImage, int width, int height)
{
Contract.Requires(sourceImage != null);
Contract.Requires(width >= 0);
Contract.Requires(height >= 0);
var scaledImage = new Bitmap(width, height, PixelFormat.Format32bppArgb);
using var g = Graphics.FromImage(scaledImage);
g.Clear(Color.Transparent);
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
var sourceWidth = sourceImage.Width;
var sourceHeight = sourceImage.Height;
var interpolationMode = InterpolationMode.HighQualityBicubic;
if (sourceWidth > 0 && sourceHeight > 0)
{
if ((width % sourceWidth) == 0 && (height % sourceHeight) == 0)
{
interpolationMode = InterpolationMode.NearestNeighbor;
}
}
g.InterpolationMode = interpolationMode;
var srcRect = new RectangleF(0.0f, 0.0f, sourceWidth, sourceHeight);
var destRect = new RectangleF(0.0f, 0.0f, width, height);
AdjustScaleRects(ref srcRect, ref destRect);
g.DrawImage(sourceImage, destRect, srcRect, GraphicsUnit.Pixel);
return scaledImage;
}
private static void AdjustScaleRects(ref RectangleF srcRect, ref RectangleF destRect)
{
if (destRect.Width > srcRect.Width)
srcRect.X -= 0.5f;
if (destRect.Height > srcRect.Height)
srcRect.Y -= 0.5f;
if (destRect.Width < srcRect.Width)
srcRect.X += 0.5f;
if (destRect.Height < srcRect.Height)
srcRect.Y += 0.5f;
}
}
}