-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageProc.cs
145 lines (119 loc) · 4.92 KB
/
ImageProc.cs
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
namespace GameOCRTTS
{
internal static class ImageProc
{
internal static Bitmap CaptureScreenshot(Rectangle bounds)
{
Bitmap bitmap = new Bitmap(bounds.Width - 200, bounds.Height - 80);
using (Graphics g = Graphics.FromImage(bitmap))
{
// Copy screen with no Windows Border
g.CopyFromScreen(new Point(bounds.Left, bounds.Top + 50), Point.Empty, bounds.Size);
}
return bitmap;
}
internal static Bitmap CropImage(Image original, Rectangle bounds)
{
Bitmap bitmap = new Bitmap(bounds.Width + 200, bounds.Height + 200);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(original,
10, 10,
new Rectangle(bounds.Left, bounds.Top, bounds.Width, bounds.Height),
GraphicsUnit.Pixel
);
}
return bitmap;
}
internal static Color GetColorFromCurrentPixel()
{
Bitmap bitmap = new Bitmap(10, 10);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(new Point(Cursor.Position.X - 1, Cursor.Position.Y - 1), Point.Empty, bitmap.Size);
}
Color result = bitmap.GetPixel(1, 1);
return result;
}
internal static unsafe Bitmap StripColorsFromImage(Image original, Color brightest, int distance)
{
int width = original.Width;
int height = original.Height;
Bitmap image = new Bitmap(width, height, PixelFormat.Format32bppPArgb);
using (Graphics gr = Graphics.FromImage(image))
gr.DrawImage(original, new Rectangle(0, 0, width, height));
int bytesPerPixel = 4; // R, G, B, A
int maxPointerLenght = width * height * bytesPerPixel;
int stride = width * bytesPerPixel;
byte R, G, B, A;
BitmapData bData = image.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite, original.PixelFormat);
byte* scan0 = (byte*)bData.Scan0.ToPointer();
for (int i = 0; i < maxPointerLenght; i += 4)
{
B = scan0[i + 0];
G = scan0[i + 1];
R = scan0[i + 2];
A = scan0[i + 3];
// Convert all white pixesls to black.
if (ColorInRange(R, G, B, brightest, distance))
{
scan0[i + 0] = 0;
scan0[i + 1] = 0;
scan0[i + 2] = 0;
}
// Remove anything else
else
{
scan0[i + 0] = 255;
scan0[i + 1] = 255;
scan0[i + 2] = 255;
}
}
image.UnlockBits(bData);
return image;
}
private static bool ColorInRange(byte r, byte g, byte b, Color brightest, int distance)
{
byte br = brightest.R;
byte bg = brightest.G;
byte bb = brightest.B;
bool result =
br - r >= 0 && br - r <= distance &&
bg - g >= 0 && bg - g <= distance &&
bb - b >= 0 && bb - b <= distance;
return result;
}
internal static Image Rescale(Image image, int dpiX, int dpiY)
{
Bitmap bm = new Bitmap((int)(image.Width * dpiX / image.HorizontalResolution), (int)(image.Height * dpiY / image.VerticalResolution));
bm.SetResolution(dpiX, dpiY);
Graphics g = Graphics.FromImage(bm);
g.InterpolationMode = InterpolationMode.Bicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(image, 0, 0);
g.Dispose();
return bm;
}
internal static string ColorToText(Color color)
{
int R = color.R;
int G = color.G;
int B = color.B;
var colorprops = typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static);
var colors = new List<Color>();
colors.AddRange(colorprops.Select(x => (Color)x.GetValue(null, null)).Where(x => x.Name != Color.Transparent.Name).ToList());
Color nearest = colors.OrderBy(x => Math.Abs(x.R - R) + Math.Abs(x.G - G) + Math.Abs(x.B - B)).First();
return nearest.Name;
}
}
}