forked from Hitachi-Momoka/krkrfgformat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPicture.cs
358 lines (348 loc) · 11.1 KB
/
Picture.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace Li.Drawing
{
public class Picture
{
public struct ARGB
{
public byte A
{
get;
set;
}
public byte R
{
get;
set;
}
public byte G
{
get;
set;
}
public byte B
{
get;
set;
}
public ARGB(byte a, byte r, byte g, byte b)
{
A = a;
R = r;
G = g;
B = b;
}
}
/// <summary>
/// 获取图片的有效区所在的Rectangle。默认留空白4
/// </summary>
/// <param name="pic">需要检测的图片。</param>
/// <returns>范围</returns>
public static Rectangle GetRectFromPictureWithouBlank(Bitmap pic)
{
return GetRectFromPictureWithoutBlankUsBitmapData(pic, 4);
}
[Obsolete("性能太慢。")]
public static Rectangle GetRectFromPictureWithoutBlank(Bitmap pic, int keep)
{
int num = 0;
int num2 = 0;
int width = pic.Width;
int height = pic.Height;
for (int i = 0; i < width; i++)
{
bool flag = false;
for (int j = 0; j < height - 1; j++)
{
if (Convert.ToInt32(pic.GetPixel(i, j).A) != 0)
{
num = i;
flag = true;
break;
}
}
if (flag)
{
break;
}
}
for (int k = 0; k < height; k++)
{
bool flag2 = false;
for (int l = 0; l < width - 1; l++)
{
if (Convert.ToInt32(pic.GetPixel(l, k).A) != 0)
{
num2 = k;
flag2 = true;
break;
}
}
if (flag2)
{
break;
}
}
int num3 = 0;
int num4 = 0;
for (int num5 = width - 1; num5 >= 0; num5--)
{
bool flag3 = false;
for (int num6 = height - 1; num6 >= 0; num6--)
{
if (Convert.ToInt32(pic.GetPixel(num5, num6).A) != 0)
{
num3 = num5;
flag3 = true;
break;
}
}
if (flag3)
{
break;
}
}
for (int num7 = height - 1; num7 >= 0; num7--)
{
bool flag4 = false;
for (int num8 = width - 1; num8 >= 0; num8--)
{
if (Convert.ToInt32(pic.GetPixel(num8, num7).A) != 0)
{
num4 = num7;
flag4 = true;
break;
}
}
if (flag4)
{
break;
}
}
Rectangle result = default(Rectangle);
if (num >= keep)
{
result.X = num - keep;
}
else
{
result.X = num;
}
if (num2 >= keep)
{
result.Y = num2 - keep;
}
else
{
result.Y = num2;
}
if (num3 >= width - keep)
{
result.Width = num3 - result.X + 1;
}
else
{
result.Width = num3 - result.X + keep + 1;
}
if (num4 >= height - keep)
{
result.Height = num4 - result.Y + 1;
}
else
{
result.Height = num4 - result.Y + keep + 1;
}
return result;
}
/// <summary>
/// 获取图片的有效区所在的Rectangle。
/// </summary>
/// <param name="pic">需要检测的图片。</param>
/// <param name="keep">需要保留的边的像素。</param>
/// <returns>范围</returns>
public static Rectangle GetRectFromPictureWithoutBlankUsBitmapData(Bitmap pic, int keep)
{
int num = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int width = pic.Width;
int height = pic.Height;
BitmapData bitmapData = pic.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, pic.PixelFormat);
IntPtr scan = bitmapData.Scan0;
int num5 = Math.Abs(bitmapData.Stride) * height;
byte[] array = new byte[num5];
Marshal.Copy(scan, array, 0, num5);
for (int i = 0; i < bitmapData.Stride; i += 4)
{
bool flag = false;
for (int j = 0; j < height - 1; j++)
{
if (array[bitmapData.Stride * j + i + 3] != 0)
{
num = i / 4;
flag = true;
break;
}
}
if (flag)
{
break;
}
}
for (int k = 0; k < height; k++)
{
bool flag2 = false;
for (int l = 0; l < bitmapData.Stride - 1; l += 4)
{
if (array[bitmapData.Stride * k + l + 3] != 0)
{
num2 = k;
flag2 = true;
break;
}
}
if (flag2)
{
break;
}
}
for (int num6 = bitmapData.Stride; num6 > 0; num6 -= 4)
{
bool flag3 = false;
for (int num7 = height - 1; num7 > 0; num7--)
{
if (array[bitmapData.Stride * num7 + num6 - 1] != 0)
{
num3 = num6 / 4;
flag3 = true;
break;
}
}
if (flag3)
{
break;
}
}
for (int num8 = height - 1; num8 > 0; num8--)
{
bool flag4 = false;
for (int num9 = bitmapData.Stride; num9 > 0; num9 -= 4)
{
if (array[bitmapData.Stride * num8 + num9 - 1] != 0)
{
num4 = num8 + 1;
flag4 = true;
break;
}
}
if (flag4)
{
break;
}
}
pic.UnlockBits(bitmapData);
Rectangle result = default;
if (num >= keep)
{
result.X = num - keep;
}
else
{
result.X = num;
}
if (num2 >= keep)
{
result.Y = num2 - keep;
}
else
{
result.Y = num2;
}
if (num3 >= width - keep)
{
result.Width = num3 - result.X;
}
else
{
result.Width = num3 - result.X + keep;
}
if (num4 >= height - keep)
{
result.Height = num4 - result.Y;
}
else
{
result.Height = num4 - result.Y + keep;
}
return result;
}
public static Bitmap CreatBackImage(int w, int h)
{
Bitmap bitmap = new Bitmap(w, h);
Bitmap BlackBlock = new Bitmap(10, 10);
using (Graphics gh = Graphics.FromImage(BlackBlock))
{
gh.FillRectangle(Brushes.Gray, new Rectangle(0, 0, 10, 10));
}
Bitmap WhiteBlock = new Bitmap(10, 10);
using (Graphics gh = Graphics.FromImage(WhiteBlock))
{
gh.FillRectangle(Brushes.White, new Rectangle(0, 0, 10, 10));
}
using (Graphics g = Graphics.FromImage(bitmap))
{
g.Clear(Color.FromArgb(0, 0, 0, 0));
for (int k = 0; k <= h / 10; k++)
{
for (int c = 0; c <= w / (10 + 10); c++)
{
if (k % 2 == 1)
{
g.DrawImage(BlackBlock, new Point(0 + 20 * c + 10,
0 + 10 * k));
g.DrawImage(WhiteBlock, new Point(0 + c * 20,
0 + 10 * k));
}
else
{
g.DrawImage(BlackBlock, new Point(0 + c * 20,
0 + 10 * k));
g.DrawImage(WhiteBlock, new Point(0 + 20 * c + 10,
0 + 10 * k));
}
}
}
}
return bitmap;
}
/// <summary>
/// 获取alpha透明的ImageAttributes。叠加用。
/// </summary>
/// <param name="opcity">透明度。(0 ~ 255)</param>
/// <returns></returns>
public static ImageAttributes GetAlphaImgAttr(int opcity)
{
if (opcity < 0 || opcity > 255)
{
throw new ArgumentOutOfRangeException("opcity 值为 0~255");
}
//颜色矩阵
float[][] matrixItems =
{
new float[]{1,0,0,0,0},
new float[]{0,1,0,0,0},
new float[]{0,0,1,0,0},
new float[]{0,0,0,(float)opcity / 255,0},
new float[]{0,0,0,0,1}
};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
ImageAttributes imageAtt = new ImageAttributes();
imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
return imageAtt;
}
}
}