该类是生成一个验证码的类。集合了网上大部分的C#关于GDI+的文章进行多次改进,现在已经形成了可在生产环节中使用的验证码。
该验证码加入了背景噪点,背景噪点曲线和直线,背景噪点文字以及扭曲,调暗,模糊等。完全可以实现防识别。
对安全性要求比较高的网站尤其适用。
同时该类还还收集了GDI+的图像处理方面的函数,包括雾化,扭曲,水波,锐化,高斯模糊,画直线,画曲线生成随机颜色,缩放图片,柔化图片,图片黑白化,增加曝光度,RGB滤镜,绘制圆角等功能。
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844 |
using
System; using
System.Collections.Generic; using
System.Text; using
System.IO; using
System.Drawing; using
System.Drawing.Drawing2D; using
System.Drawing.Text; using
System.Drawing.Imaging; /********************** * 验证码生成类 * 作者:李飞麟 * URL:http://www.xuehuwang.com * Email:lifei6671@163.com * * *********************/ namespace
NS.DrawValidationCode { #region 验证码生成类 /// <summary> /// 验证码生成类 /// </summary> public
class DrawValidationCode { #region 定义和初始化配置字段 //用户存取验证码字符串 private
string validationCode = String.Empty; /// <summary> /// 获取系统生成的随机验证码 /// </summary> public
String ValidationCode { get
{ return
validationCode; } } private
Int32 validationCodeCount = 4; /// <summary> /// 获取和设置验证码字符串的长度 /// </summary> public
Int32 ValidationCodeCount { get
{ return
validationCodeCount; } set
{ validationCodeCount = value; } } Graphics dc = null ; private
int bgWidth = 130; /// <summary> /// 验证码的宽度,默认为80 /// </summary> public
Int32 Width { get
{ return
bgWidth; } set
{ bgWidth = value; } } private
int bgHeight = 40; /// <summary> /// 验证码的高度,默认为40 /// </summary> public
Int32 Height { get
{ return
bgHeight; } set
{ bgHeight = value; } } /* private string[] fontFace = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" }; /// <summary> /// 验证码字体列表,默认为{ "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" } /// </summary> public String[] FontFace { get { return fontFace; } set { fontFace = value; } }*/ private
int fontMinSize = 15; /// <summary> /// 验证码字体的最小值,默认为15,建议不小于15像素 /// </summary> public
Int32 FontMinSize { get
{ return
fontMinSize; } set
{ fontMinSize = value; } } private
Int32 fontMaxSize = 20; /// <summary> /// 验证码字体的最大值,默认为20 /// </summary> public
Int32 FontMaxSize { get
{ return
fontMaxSize; } set
{ fontMaxSize = value; } } private
Color[] fontColor = { }; /// <summary> /// 验证码字体的颜色,默认为系统自动生成字体颜色 /// </summary> public
Color[] FontColor { get
{ return
fontColor; } set
{ fontColor = value; } } private
Color backColor = Color.FromArgb(243, 255, 255); /// <summary> /// 验证码的背景色,默认为Color.FromArgb(243, 251, 254) /// </summary> public
Color BackgroundColor { get
{ return
backColor; } set
{ backColor = value; } } private
Int32 bezierCount = 3; /// <summary> /// 贝塞尔曲线的条数,默认为3条 /// </summary> public
Int32 BezierCount { get
{ return
bezierCount; } set
{ bezierCount = value; } } private
Int32 lineCount = 3; /// <summary> /// 直线条数,默认为3条 /// </summary> public
Int32 LineCount { get
{ return
lineCount; } set
{ lineCount = value; } } Random random; private
String charCollection = "2,3,4,5,6,7,8,9,a,s,d,f,g,h,z,c,v,b,n,m,k,q,w,e,r,t,y,u,p,A,S,D,F,G,H,Z,C,V,B,N,M,K,Q,W,E,R,T,Y,U,P" ; //定义验证码字符及出现频次 ,避免出现0 o j i l 1 x; /// <summary> /// 随机字符串列表,请使用英文状态下的逗号分隔。 /// </summary> public
String CharCollection { get
{ return
charCollection; } set
{ charCollection = value; } } private
Int32 intCount = 4; /// <summary> /// 验证码字符串个数,默认为4个字符 /// </summary> public
Int32 IntCount { get
{ return
intCount; } set
{ intCount = value; } } private
Boolean isPixel = true ; /// <summary> /// 是否添加噪点,默认添加,噪点颜色为系统随机生成。 /// </summary> public
Boolean IsPixel { get
{ return
isPixel; } set
{ isPixel = value; } } private
Boolean isRandString = true ; /// <summary> /// 是否添加随机噪点字符串,默认添加 /// </summary> public
Boolean IsRandString { get
{ return
isRandString; } set
{ isRandString = value; } } /// <summary> /// 随机背景字符串的个数 /// </summary> public
Int32 RandomStringCount { get ; set ; } private
Int32 randomStringFontSize = 9; /// <summary> /// 随机背景字符串的大小 /// </summary> public
Int32 RandomStringFontSize { get
{ return
randomStringFontSize; } set
{ randomStringFontSize = value; } } /// <summary> /// 是否对图片进行扭曲 /// </summary> public
Boolean IsTwist { get ; set ; } /// <summary> /// 边框样式 /// </summary> public
enum BorderStyle { /// <summary> /// 无边框 /// </summary> None, /// <summary> /// 矩形边框 /// </summary> Rectangle, /// <summary> /// 圆角边框 /// </summary> RoundRectangle } private
Int32 rotationAngle = 40; /// <summary> /// 验证码字符串随机转动的角度的最大值 /// </summary> public
Int32 RotationAngle { get
{ return
rotationAngle; } set
{ rotationAngle = value; } } /// <summary> /// 设置或获取边框样式 /// </summary> public
BorderStyle Border { get ; set ; } private
Point[] strPoint = null ; private
Double gaussianDeviation = 0; /// <summary> /// 对验证码图片进行高斯模糊的阀值,如果设置为0,则不对图片进行高斯模糊,该设置可能会对图片处理的性能有较大影响 /// </summary> public
Double GaussianDeviation { get
{ return
gaussianDeviation; } set
{ gaussianDeviation = value; } } private
Int32 brightnessValue = 0; /// <summary> /// 对图片进行暗度和亮度的调整,如果该值为0,则不调整。该设置会对图片处理性能有较大影响 /// </summary> public
Int32 BrightnessValue { get
{ return
brightnessValue; } set
{ brightnessValue = value; } } #endregion /// <summary> /// 构造函数,用于初始化常用变量 /// </summary> public
DrawValidationCode() { random = new
Random(Guid.NewGuid().GetHashCode()); strPoint = new
Point[validationCodeCount + 1]; if
(gaussianDeviation < 0) gaussianDeviation = 0; } /// <summary> /// 生成验证码 /// </summary> /// <param name="target">用于存储图片的一般字节序列</param> public
void CreateImage(Stream target) { Bitmap bit = new
Bitmap(bgWidth + 1, bgHeight + 1); //写字符串 dc = Graphics.FromImage(bit); dc.SmoothingMode = SmoothingMode.HighQuality; dc.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; ; dc.InterpolationMode = InterpolationMode.HighQualityBilinear; dc.CompositingQuality = CompositingQuality.HighQuality; dc.Clear(Color.White); dc.DrawImageUnscaled(DrawBackground(), 0, 0); dc.DrawImageUnscaled(DrawRandomString(), 0, 0); //对图片文字进行扭曲 bit = AdjustRippleEffect(bit, 5); //对图片进行高斯模糊 if
(gaussianDeviation > 0) { Gaussian gau = new
Gaussian(); bit = gau.FilterProcessImage(gaussianDeviation, bit); } //进行暗度和亮度处理 if
(brightnessValue != 0) { //对图片进行调暗处理 bit = AdjustBrightness(bit, brightnessValue); } bit.Save(target, ImageFormat.Gif); //brush.Dispose(); bit.Dispose(); dc.Dispose(); } #region 画验证码背景,例如,增加早点,添加曲线和直线等 /// <summary> /// 画验证码背景,例如,增加早点,添加曲线和直线等 /// </summary> /// <returns></returns> private
Bitmap DrawBackground() { Bitmap bit = new
Bitmap(bgWidth + 1, bgHeight + 1); Graphics g = Graphics.FromImage(bit); g.SmoothingMode = SmoothingMode.HighQuality; g.Clear(Color.White); Rectangle rectangle = new
Rectangle(0, 0, bgWidth, bgHeight); Brush brush = new
SolidBrush(backColor); g.FillRectangle(brush, rectangle); //画噪点 if
(isPixel) { g.DrawImageUnscaled(DrawRandomPixel(30), 0, 0); } g.DrawImageUnscaled(DrawRandBgString(), 0, 0); //画曲线 g.DrawImageUnscaled(DrawRandomBezier(bezierCount), 0, 0); //画直线 g.DrawImageUnscaled(DrawRandomLine(lineCount), 0, 0); //dc.DrawImageUnscaled(DrawStringline(), 0, 0); if
(Border == BorderStyle.Rectangle) { //绘制边框 g.DrawRectangle( new
Pen(Color.FromArgb(90, 87, 46)), 0, 0, bgWidth, bgHeight); } else
if (Border == BorderStyle.RoundRectangle) { //画圆角 DrawRoundRectangle(g, rectangle, Color.FromArgb(90, 87, 46), 1, 3); } return
bit; } #endregion #region 画正弦曲线 private
Bitmap DrawTwist(Bitmap bmp, Int32 tWidth, Int32 tHeight, float
angle, Color color) { //为了方便查看效果,在这里我定义了一个常量。 //它在定义数组的长度和for循环中都要用到。 int
size = bgWidth; double [] x = new
double [size]; Bitmap b = new
Bitmap(bmp.Width, bmp.Height); b.MakeTransparent(); Graphics graphics = Graphics.FromImage(b); Pen pen = new
Pen(color); //画正弦曲线的横轴间距参数。建议所用的值应该是 正数且是2的倍数。 //在这里采用2。 int
val = 2; float
temp = 0.0f; //把画布下移100。为什么要这样做,只要你把这一句给注释掉,运行一下代码, //你就会明白是为什么? graphics.TranslateTransform(0, 100); graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; for
( int i = 0; i < size; i++) { //改变tWidth,实现正弦曲线宽度的变化。 //改tHeight,实现正弦曲线高度的变化。 x[i] = Math.Sin(2 * Math.PI * i / tWidth) * tHeight; graphics.DrawLine(pen, i * val, temp, i * val + val / 2, ( float )x[i]); temp = ( float )x[i]; } graphics.RotateTransform(60, MatrixOrder.Prepend); //旋转图片 // b = KiRotate(b, angle, Color.Transparent); return
b; } #endregion #region 正弦曲线Wave扭曲图片 /// <summary> /// 正弦曲线Wave扭曲图片 /// </summary> /// <param name="srcBmp">图片路径</param> /// <param name="bXDir">如果扭曲则选择为True</param> /// <param name="dMultValue">波形的幅度倍数,越大扭曲的程度越高,一般为3</param> /// <param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param> /// <returns></returns> public
Bitmap TwistImage(Bitmap srcBmp, bool
bXDir, double
dMultValue, double
dPhase) { System.Drawing.Bitmap destBmp = new
Bitmap(srcBmp.Width, srcBmp.Height); double
PI2 = 6.283185307179586476925286766559; // 将位图背景填充为白色 System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp); graph.FillRectangle( new
SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height); graph.Dispose(); double
dBaseAxisLen = bXDir ? ( double )destBmp.Height : ( double )destBmp.Width; for
( int i = 0; i < destBmp.Width; i++) { for
( int j = 0; j < destBmp.Height; j++) { double
dx = 0; dx = bXDir ? (PI2 * ( double )j) / dBaseAxisLen : (PI2 * ( double )i) / dBaseAxisLen; dx += dPhase; double
dy = Math.Sin(dx); // 取得当前点的颜色 int
nOldX = 0, nOldY = 0; nOldX = bXDir ? i + ( int )(dy * dMultValue) : i; nOldY = bXDir ? j : j + ( int )(dy * dMultValue); System.Drawing.Color color = srcBmp.GetPixel(i, j); if
(nOldX >= 0 && nOldX < destBmp.Width && nOldY >= 0 && nOldY < destBmp.Height) { destBmp.SetPixel(nOldX, nOldY, color); } } } return
destBmp; } #endregion #region 图片任意角度旋转 /// <summary> /// 图片任意角度旋转 /// </summary> /// <param name="bmp">原始图Bitmap</param> /// <param name="angle">旋转角度</param> /// <param name="bkColor">背景色</param> /// <returns>输出Bitmap</returns> public
static Bitmap KiRotate(Bitmap bmp, float
angle, Color bkColor) { int
w = bmp.Width; int
h = bmp.Height; PixelFormat pf; if
(bkColor == Color.Transparent) { pf = PixelFormat.Format32bppArgb; } else { pf = bmp.PixelFormat; } Bitmap tmp = new
Bitmap(w, h, pf); Graphics g = Graphics.FromImage(tmp); g.Clear(bkColor); g.DrawImageUnscaled(bmp, 1, 1); g.Dispose(); GraphicsPath path = new
GraphicsPath(); path.AddRectangle( new
RectangleF(0f, 0f, w, h)); Matrix mtrx = new
Matrix(); mtrx.Rotate(angle); RectangleF rct = path.GetBounds(mtrx); Bitmap dst = new
Bitmap(( int )rct.Width, ( int )rct.Height, pf); g = Graphics.FromImage(dst); g.Clear(bkColor); g.TranslateTransform(-rct.X, -rct.Y); g.RotateTransform(angle); g.InterpolationMode = InterpolationMode.HighQualityBilinear; g.DrawImageUnscaled(tmp, 0, 0); g.Dispose(); tmp.Dispose(); return
dst; } #endregion #region 随机生成贝塞尔曲线 /// <summary> /// 随机生成贝塞尔曲线 /// </summary> /// <param name="bmp">一个图片的实例</param> /// <param name="lineNum">线条数量</param> /// <returns></returns> public
Bitmap DrawRandomBezier(Int32 lineNum) { Bitmap b = new
Bitmap(bgWidth, bgHeight); b.MakeTransparent(); Graphics g = Graphics.FromImage(b); g.Clear(Color.Transparent); g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; GraphicsPath gPath1 = new
GraphicsPath(); Int32 lineRandNum = random.Next(lineNum); for
( int i = 0; i < (lineNum - lineRandNum); i++) { Pen p = new
Pen(GetRandomDeepColor()); Point[] point = { new
Point(random.Next(1, (b.Width / 10)), random.Next(1, (b.Height))), new
Point(random.Next((b.Width / 10) * 2, (b.Width / 10) * 4), random.Next(1, (b.Height))), new
Point(random.Next((b.Width / 10) * 4, (b.Width / 10) * 6), random.Next(1, (b.Height))), new
Point(random.Next((b.Width / 10) * 8, b.Width), random.Next(1, (b.Height))) }; gPath1.AddBeziers(point); g.DrawPath(p, gPath1); p.Dispose(); } for
( int i = 0; i < lineRandNum; i++) { Pen p = new
Pen(GetRandomDeepColor()); Point[] point = { new
Point(random.Next(1, b.Width), random.Next(1, b.Height)), new
Point(random.Next((b.Width / 10) * 2, b.Width), random.Next(1, b.Height)), new
Point(random.Next((b.Width / 10) * 4, b.Width), random.Next(1, b.Height)), new
Point(random.Next(1, b.Width), random.Next(1, b.Height)) }; gPath1.AddBeziers(point); g.DrawPath(p, gPath1); p.Dispose(); } return
b; } #endregion #region 画直线 /// <summary> /// 画直线 /// </summary> /// <param name="bmp">一个bmp实例</param> /// <param name="lineNum">线条个数</param> /// <returns></returns> public
Bitmap DrawRandomLine(Int32 lineNum) { if
(lineNum < 0) throw
new ArgumentNullException( "参数bmp为空!" ); Bitmap b = new
Bitmap(bgWidth, bgHeight); b.MakeTransparent(); Graphics g = Graphics.FromImage(b); g.Clear(Color.Transparent); g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; for
( int i = 0; i < lineNum; i++) { Pen p = new
Pen(GetRandomDeepColor()); Point pt1 = new
Point(random.Next(1, (b.Width / 5) * 2), random.Next(b.Height)); Point pt2 = new
Point(random.Next((b.Width / 5) * 3, b.Width), random.Next(b.Height)); g.DrawLine(p, pt1, pt2); p.Dispose(); } return
b; } #endregion #region 画随机噪点 /// <summary> /// 画随机噪点 /// </summary> /// <param name="pixNum">噪点的百分比</param> /// <returns></returns> public
Bitmap DrawRandomPixel(Int32 pixNum) { Bitmap b = new
Bitmap(bgWidth, bgHeight); b.MakeTransparent(); Graphics graph = Graphics.FromImage(b); graph.SmoothingMode = SmoothingMode.HighQuality; graph.InterpolationMode = InterpolationMode.HighQualityBilinear; //画噪点 for
( int i = 0; i < (bgHeight * bgWidth) / pixNum; i++) { int
x = random.Next(b.Width); int
y = random.Next(b.Height); b.SetPixel(x, y, GetRandomDeepColor()); //下移坐标重新画点 if
((x + 1) < b.Width && (y + 1) < b.Height) { //画图片的前景噪音点 graph.DrawRectangle( new
Pen(Color.Silver), random.Next(b.Width), random.Next(b.Height), 1, 1); } } return
b; } #endregion #region 画随机字符串中间连线 /// <summary> /// 画随机字符串中间连线 /// </summary> /// <returns></returns> private
Bitmap DrawStringline() { Bitmap b = new
Bitmap(bgWidth, bgHeight); b.MakeTransparent(); Graphics g = Graphics.FromImage(b); g.SmoothingMode = SmoothingMode.AntiAlias; Point[] p = new
Point[validationCodeCount]; for
( int i = 0; i < validationCodeCount; i++) { p[i] = strPoint[i]; //throw new Exception(strPoint.Length.ToString()); } // g.DrawBezier(new Pen(GetRandomDeepColor()), strPoint); //g.DrawClosedCurve(new Pen(GetRandomDeepColor()), strPoint); g.DrawCurve( new
Pen(GetRandomDeepColor(), 1), strPoint); return
b; } #endregion #region 写入验证码的字符串 /// <summary> /// 写入验证码的字符串 /// </summary> private
Bitmap DrawRandomString() { if
(fontMaxSize >= (bgHeight / 5) * 4) throw
new ArgumentException( "字体最大值参数FontMaxSize与验证码高度相近,这会导致描绘验证码字符串时出错,请重新设置参数!" ); Bitmap b = new
Bitmap(bgWidth, bgHeight); b.MakeTransparent(); Graphics g = Graphics.FromImage(b); g.Clear(Color.Transparent); g.PixelOffsetMode = PixelOffsetMode.Half; g.SmoothingMode = SmoothingMode.HighQuality; g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit; g.InterpolationMode = InterpolationMode.HighQualityBilinear; char [] chars = GetRandomString(validationCodeCount).ToCharArray(); //拆散字符串成单字符数组 validationCode = chars.ToString(); //设置字体显示格式 StringFormat format = new
StringFormat(StringFormatFlags.NoClip); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; FontFamily f = new
FontFamily(GenericFontFamilies.Monospace); Int32 charNum = chars.Length; Point sPoint = new
Point(); Int32 fontSize = 9; for
( int i = 0; i < validationCodeCount; i++) { int
findex = random.Next(5); //定义字体 Font textFont = new
Font(f, random.Next(fontMinSize, fontMaxSize), FontStyle.Bold); //定义画刷,用于写字符串 //Brush brush = new SolidBrush(GetRandomDeepColor()); Int32 textFontSize = Convert.ToInt32(textFont.Size); fontSize = textFontSize; Point point = new
Point(random.Next((bgWidth / charNum) * i + 5, (bgWidth / charNum) * (i + 1)), random.Next(bgHeight / 5 + textFontSize / 2, bgHeight - textFontSize / 2)); //如果当前字符X坐标小于字体的二分之一大小 if
(point.X < textFontSize / 2) { point.X = point.X + textFontSize / 2; } //防止文字叠加 if
(i > 0 && (point.X - sPoint.X < (textFontSize / 2 + textFontSize / 2))) { point.X = point.X + textFontSize; } //如果当前字符X坐标大于图片宽度,就减去字体的宽度 if
(point.X > (bgWidth - textFontSize / 2)) { point.X = bgWidth - textFontSize / 2; } sPoint = point; float
angle = random.Next(-rotationAngle, rotationAngle); //转动的度数 g.TranslateTransform(point.X, point.Y); //移动光标到指定位置 g.RotateTransform(angle); //设置渐变画刷 Rectangle myretang = new
Rectangle(0, 1, Convert.ToInt32(textFont.Size), Convert.ToInt32(textFont.Size)); Color c = GetRandomDeepColor(); LinearGradientBrush mybrush2 = new
LinearGradientBrush(myretang, c, GetLightColor(c, 120), random.Next(180)); g.DrawString(chars[i].ToString(), textFont, mybrush2, 1, 1, format); g.RotateTransform(-angle); //转回去 g.TranslateTransform(-point.X, -point.Y); //移动光标到指定位置,每个字符紧凑显示,避免被软件识别 strPoint[i] = point; textFont.Dispose(); mybrush2.Dispose(); } return
b; } #endregion #region 画干扰背景文字 /// <summary> /// 画背景干扰文字 /// </summary> /// <returns></returns> private
Bitmap DrawRandBgString() { Bitmap b = new
Bitmap(bgWidth, bgHeight); String[] randStr = { "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" , "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" , "y" , "z" , "A" , "B" , "C" , "D" , "E" , "F" , "G" , "H" , "I" , "J" , "K" , "L" , "M" , "N" , "O" , "P" , "Q" , "R" , "S" , "T" , "U" , "V" , "W" , "X" , "Y" , "Z"
}; b.MakeTransparent(); Graphics g = Graphics.FromImage(b); g.Clear(Color.Transparent); g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.TextRenderingHint = TextRenderingHint.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBilinear; //设置字体显示格式 StringFormat format = new
StringFormat(StringFormatFlags.NoClip); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; FontFamily f = new
FontFamily(GenericFontFamilies.Serif); Font textFont = new
Font(f, randomStringFontSize, FontStyle.Underline); int
randAngle = 60; //随机转动角度 for
( int i = 0; i < RandomStringCount; i++) { Brush brush = new
System.Drawing.SolidBrush(GetRandomLightColor()); Point pot = new
Point(random.Next(5, bgWidth - 5), random.Next(5, bgHeight - 5)); //随机转动的度数 float
angle = random.Next(-randAngle, randAngle); //转动画布 g.RotateTransform(angle); g.DrawString(randStr[random.Next(randStr.Length)], textFont, brush, pot, format); //转回去,为下一个字符做准备 g.RotateTransform(-angle); //释放资源 brush.Dispose(); } textFont.Dispose(); format.Dispose(); f.Dispose(); return
b; } #endregion #region 生成随机字符串 /// <summary> /// 生成随机字符串 /// </summary> /// <returns></returns> private
string GetRandomString(Int32 textLength) { string [] randomArray = charCollection.Split( ‘,‘ ); //将字符串生成数组 int
arrayLength = randomArray.Length; string
randomString = "" ; for
( int i = 0; i < textLength; i++) { randomString += randomArray[random.Next(0, arrayLength)]; } return
randomString; //长度是textLength +1 } #endregion #region 内部方法:绘制验证码背景 private
void DrawBackground(HatchStyle hatchStyle) { //设置填充背景时用的笔刷 HatchBrush hBrush = new
HatchBrush(hatchStyle, backColor); //填充背景图片 dc.FillRectangle(hBrush, 0, 0, this .bgWidth, this .bgHeight); } #endregion #region 根据指定长度,返回随机验证码 /// <summary> /// 根据指定长度,返回随机验证码 /// </summary> /// <param >制定长度</param> /// <returns>随即验证码</returns> public
string Next( int
length) { this .validationCode = GetRandomCode(length); return
this .validationCode; } #endregion #region 内部方法:返回指定长度的随机验证码字符串 /// <summary> /// 根据指定大小返回随机验证码 /// </summary> /// <param >字符串长度</param> /// <returns>随机字符串</returns> private
string GetRandomCode( int
length) { StringBuilder sb = new
StringBuilder(6); for
( int i = 0; i < length; i++) { sb.Append(Char.ConvertFromUtf32(RandomAZ09())); } return
sb.ToString(); } #endregion #region 内部方法:产生随机数和随机点 /// <summary> /// 产生0-9A-Z的随机字符代码 /// </summary> /// <returns>字符代码</returns> private
int RandomAZ09() { int
result = 48; Random ram = new
Random(); int
i = ram.Next(2); switch
(i) { case
0: result = ram.Next(48, 58); break ; case
1: result = ram.Next(65, 91); break ; } return
result; } /// <summary> /// 返回一个随机点,该随机点范围在验证码背景大小范围内 /// </summary> /// <returns>Point对象</returns> private
Point RandomPoint() { Random ram = new
Random(); Point point = new
Point(ram.Next( this .bgWidth), ram.Next( this .bgHeight)); return
point; } #endregion #region 随机生成颜色值 /// <summary> /// 生成随机深颜色 /// </summary> /// <returns></returns> public
Color GetRandomDeepColor() { int
nRed, nGreen, nBlue; // nBlue,nRed nGreen 相差大一点 nGreen 小一些 //int high = 255; int
redLow = 160; int
greenLow = 100; int
blueLow = 160; nRed = random.Next(redLow); nGreen = random.Next(greenLow); nBlue = random.Next(blueLow); Color color = Color.FromArgb(nRed, nGreen, nBlue); return
color; } /// <summary> /// 生成随机浅颜色 /// </summary> /// <returns>randomColor</returns> public
Color GetRandomLightColor() { int
nRed, nGreen, nBlue; //越大颜色越浅 int
low = 180; //色彩的下限 int
high = 255; //色彩的上限 nRed = random.Next(high) % (high - low) + low; nGreen = random.Next(high) % (high - low) + low; nBlue = random.Next(high) % (high - low) + low; Color color = Color.FromArgb(nRed, nGreen, nBlue); return
color; } /// <summary> /// 生成随机颜色值 /// </summary> /// <returns></returns> public
Color GetRandomColor() { int
nRed, nGreen, nBlue; //越大颜色越浅 int
low = 10; //色彩的下限 int
high = 255; //色彩的上限 nRed = random.Next(high) % (high - low) + low; nGreen = random.Next(high) % (high - low) + low; nBlue = random.Next(high) % (high - low) + low; Color color = Color.FromArgb(nRed, nGreen, nBlue); return
color; } /// <summary> /// 获取与当前颜色值相加后的颜色 /// </summary> /// <param name="c"></param> /// <returns></returns> public
Color GetLightColor(Color c, Int32 value) { int
nRed = c.R, nGreen = c.G, nBlue = c.B; //越大颜色越浅 if
(nRed + value < 255 && nRed + value > 0) { nRed = c.R + 40; } if
(nGreen + value < 255 && nGreen + value > 0) { nGreen = c.G + 40; } if
(nBlue + value < 255 && nBlue + value > 0) { nBlue = c.B + 40; } Color color = Color.FromArgb(nRed, nGreen, nBlue); return
color; } #endregion #region 合并图片 /// <summary> /// 合并图片 /// </summary> /// <param name="maps"></param> /// <returns></returns> private
Bitmap MergerImg( params
Bitmap[] maps) { int
i = maps.Length; if
(i == 0) throw
new Exception( "图片数不能够为0" ); //创建要显示的图片对象,根据参数的个数设置宽度 Bitmap backgroudImg = new
Bitmap(i * 12, 16); Graphics g = Graphics.FromImage(backgroudImg); //清除画布,背景设置为白色 g.Clear(System.Drawing.Color.White); for
( int j = 0; j < i; j++) { //g.DrawImage(maps[j], j * 11, 0, maps[j].Width, maps[j].Height); g.DrawImageUnscaled(maps[j], 0, 0); } g.Dispose(); return
backgroudImg; } #endregion #region 生成不重复的随机数,该函数会消耗大量系统资源 /// <summary> /// 生成不重复的随机数,该函数会消耗大量系统资源 /// </summary> /// <returns></returns> private
static int GetRandomSeed() { byte [] bytes = new
byte [4]; System.Security.Cryptography.RNGCryptoServiceProvider rng = new
System.Security.Cryptography.RNGCryptoServiceProvider(); rng.GetBytes(bytes); return
BitConverter.ToInt32(bytes, 0); } #endregion #region 缩放图片 /// <summary> /// 缩放图片 /// </summary> /// <param name="bmp">原始Bitmap</param> /// <param name="newW">新的宽度</param> /// <param name="newH">新的高度</param> /// <param name="Mode">缩放质量</param> /// <returns>处理以后的图片</returns> public
static Bitmap KiResizeImage(Bitmap bmp, int
newW, int
newH, InterpolationMode Mode) { try { Bitmap b = new
Bitmap(newW, newH); Graphics g = Graphics.FromImage(b); // 插值算法的质量 g.InterpolationMode = Mode; g.DrawImage(bmp, new
Rectangle(0, 0, newW, newH), new
Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel); g.Dispose(); return
b; } catch { return
null ; } } #endregion #region 绘制圆角矩形 /// <summary> /// C# GDI+ 绘制圆角矩形 /// </summary> /// <param name="g">Graphics 对象</param> /// <param name="rectangle">Rectangle 对象,圆角矩形区域</param> /// <param name="borderColor">边框颜色</param> /// <param name="borderWidth">边框宽度</param> /// <param name="r">圆角半径</param> private
static void DrawRoundRectangle(Graphics g, Rectangle rectangle, Color borderColor, float
borderWidth, int
r) { // 如要使边缘平滑,请取消下行的注释 g.SmoothingMode = SmoothingMode.HighQuality; // 由于边框也需要一定宽度,需要对矩形进行修正 //rectangle = new Rectangle(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); Pen p = new
Pen(borderColor, borderWidth); // 调用 getRoundRectangle 得到圆角矩形的路径,然后再进行绘制 g.DrawPath(p, getRoundRectangle(rectangle, r)); } #endregion #region 根据普通矩形得到圆角矩形的路径 /// <summary> /// 根据普通矩形得到圆角矩形的路径 /// </summary> /// <param name="rectangle">原始矩形</param> /// <param name="r">半径</param> /// <returns>图形路径</returns> private
static GraphicsPath getRoundRectangle(Rectangle rectangle, int
r) { int
l = 2 * r; // 把圆角矩形分成八段直线、弧的组合,依次加到路径中 GraphicsPath gp = new
GraphicsPath(); gp.AddLine( new
Point(rectangle.X + r, rectangle.Y), new
Point(rectangle.Right - r, rectangle.Y)); gp.AddArc( new
Rectangle(rectangle.Right - l, rectangle.Y, l, l), 270F, 90F); gp.AddLine( new
Point(rectangle.Right, rectangle.Y + r), new
Point(rectangle.Right, rectangle.Bottom - r)); gp.AddArc( new
Rectangle(rectangle.Right - l, rectangle.Bottom - l, l, l), 0F, 90F); gp.AddLine( new
Point(rectangle.Right - r, rectangle.Bottom), new
Point(rectangle.X + r, rectangle.Bottom)); gp.AddArc( new
Rectangle(rectangle.X, rectangle.Bottom - l, l, l), 90F, 90F); gp.AddLine( new
Point(rectangle.X, rectangle.Bottom - r), new
Point(rectangle.X, rectangle.Y + r)); gp.AddArc( new
Rectangle(rectangle.X, rectangle.Y, l, l), 180F, 90F); return
gp; } #endregion #region 柔化 ///<summary> /// 柔化 /// </summary> /// <param name="b">原始图</param> /// <returns>输出图</returns> public
static Bitmap KiBlur(Bitmap b) { if
(b == null ) { return
null ; } int
w = b.Width; int
h = b.Height; try { Bitmap bmpRtn = new
Bitmap(w, h, PixelFormat.Format24bppRgb); BitmapData srcData = b.LockBits( new
Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); BitmapData dstData = bmpRtn.LockBits( new
Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); unsafe { byte * pIn = ( byte *)srcData.Scan0.ToPointer(); byte * pOut = ( byte *)dstData.Scan0.ToPointer(); int
stride = srcData.Stride; byte * p; for
( int y = 0; y < h; y++) { for
( int x = 0; x < w; x++) { //取周围9点的值 if
(x == 0 || x == w - 1 || y == 0 || y == h - 1) { //不做 pOut[0] = pIn[0]; pOut[1] = pIn[1]; pOut[2] = pIn[2]; } else { int
r1, r2, r3, r4, r5, r6, r7, r8, r9; int
g1, g2, g3, g4, g5, g6, g7, g8, g9; int
b1, b2, b3, b4, b5, b6, b7, b8, b9; float
vR, vG, vB; //左上 p = pIn - stride - 3; r1 = p[2]; g1 = p[1]; b1 = p[0]; //正上 p = pIn - stride; r2 = p[2]; g2 = p[1]; b2 = p[0]; //右上 p = pIn - stride + 3; r3 = p[2]; g3 = p[1]; b3 = p[0]; //左侧 p = pIn - 3; r4 = p[2]; g4 = p[1]; b4 = p[0]; //右侧 p = pIn + 3; r5 = p[2]; g5 = p[1]; b5 = p[0]; //右下 p = pIn + stride - 3; r6 = p[2]; g6 = p[1]; b6 = p[0]; //正下 p = pIn + stride; r7 = p[2]; g7 = p[1]; b7 = p[0]; //右下 p = pIn + stride + 3; r8 = p[2]; g8 = p[1]; b8 = p[0]; //自己 p = pIn; r9 = p[2]; g9 = p[1]; b9 = p[0]; vR = ( float )(r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8 + r9); vG = ( float )(g1 + g2 + g3 + g4 + g5 + g6 + g7 + g8 + g9); vB = ( float )(b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8 + b9); vR /= 9; vG /= 9; vB /= 9; pOut[0] = ( byte )vB; pOut[1] = ( byte )vG; pOut[2] = ( byte )vR; } pIn += 3; pOut += 3; } // end of x pIn += srcData.Stride - w * 3; pOut += srcData.Stride - w * 3; } // end of y } b.UnlockBits(srcData); bmpRtn.UnlockBits(dstData); return
bmpRtn; } catch { return
null ; } } // end of KiBlur #endregion #region 滤镜 /// <summary> /// 红色滤镜 /// </summary> /// <param name="bitmap">Bitmap</param> /// <param name="threshold">阀值 -255~255</param> /// <returns></returns> public
System.Drawing.Bitmap AdjustToRed(System.Drawing.Bitmap bitmap, int
threshold) { for
( int y = 0; y < bitmap.Height; y++) { for
( int x = 0; x < bitmap.Width; x++) { // 取得每一個 pixel var
pixel = bitmap.GetPixel(x, y); var
pR = pixel.R + threshold; pR = Math.Max(pR, 0); pR = Math.Min(255, pR); // 將改過的 RGB 寫回 // 只寫入紅色的值 , G B 都放零 System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixel.A, pR, 0, 0); bitmap.SetPixel(x, y, newColor); } } // 回傳結果 return
bitmap; } /// <summary> /// 绿色滤镜 /// </summary> /// <param name="bitmap">一个图片实例</param> /// <param name="threshold">阀值 -255~+255</param> /// <returns></returns> public
System.Drawing.Bitmap AdjustToGreen(System.Drawing.Bitmap bitmap, int
threshold) { for
( int y = 0; y < bitmap.Height; y++) { for
( int x = 0; x < bitmap.Width; x++) { // 取得每一個 pixel var
pixel = bitmap.GetPixel(x, y); //判斷是否超過255 如果超過就是255 var
pG = pixel.G + threshold; //如果小於0就為0 if
(pG > 255) pG = 255; if
(pG < 0) pG = 0; // 將改過的 RGB 寫回 // 只寫入綠色的值 , R B 都放零 System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixel.A, 0, pG, 0); bitmap.SetPixel(x, y, newColor); } } // 回傳結果 return
bitmap; } /// <summary> /// 蓝色滤镜 /// </summary> /// <param name="bitmap">一个图片实例</param> /// <param name="threshold">阀值 -255~255</param> /// <returns></returns> public
System.Drawing.Bitmap AdjustToBlue(System.Drawing.Bitmap bitmap, int
threshold) { for
( int y = 0; y < bitmap.Height; y++) { for
( int x = 0; x < bitmap.Width; x++) { // 取得每一個 pixel var
pixel = bitmap.GetPixel(x, y); //判斷是否超過255 如果超過就是255 var
pB = pixel.B + threshold; //如果小於0就為0 if
(pB > 255) pB = 255; if
(pB < 0) pB = 0; // 將改過的 RGB 寫回 // 只寫入藍色的值 , R G 都放零 System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixel.A, 0, 0, pB); bitmap.SetPixel(x, y, newColor); } } // 回傳結果 return
bitmap; } /// <summary> /// 调整 RGB 色调 /// </summary> /// <param name="bitmap"></param> /// <param name="thresholdRed">红色阀值</param> /// <param name="thresholdBlue">蓝色阀值</param> /// <param name="thresholdGreen">绿色阀值</param> /// <returns></returns> public
System.Drawing.Bitmap AdjustToCustomColor(System.Drawing.Bitmap bitmap, int
thresholdRed, int
thresholdGreen, int
thresholdBlue) { for
( int y = 0; y < bitmap.Height; y++) { for
( int x = 0; x < bitmap.Width; x++) { // 取得每一個 pixel var
pixel = bitmap.GetPixel(x, y); //判斷是否超過255 如果超過就是255 var
pG = pixel.G + thresholdGreen; //如果小於0就為0 if
(pG > 255) pG = 255; if
(pG < 0) pG = 0; //判斷是否超過255 如果超過就是255 var
pR = pixel.R + thresholdRed; //如果小於0就為0 if
(pR > 255) pR = 255; if
(pR < 0) pR = 0; //判斷是否超過255 如果超過就是255 var
pB = pixel.B + thresholdBlue; //如果小於0就為0 if
(pB > 255) pB = 255; if
(pB < 0) pB = 0; // 將改過的 RGB 寫回 // 只寫入綠色的值 , R B 都放零 System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixel.A, pR, pG, pB); bitmap.SetPixel(x, y, newColor); } } return
bitmap; } #endregion #region 图片去色(图片黑白化) /// <summary> /// 图片去色(图片黑白化) /// </summary> /// <param name="original">一个需要处理的图片</param> /// <returns></returns> public
static Bitmap MakeGrayscale(Bitmap original) { //create a blank bitmap the same size as original Bitmap newBitmap = new
Bitmap(original.Width, original.Height); //get a graphics object from the new image Graphics g = Graphics.FromImage(newBitmap); g.SmoothingMode = SmoothingMode.HighQuality; //create the grayscale ColorMatrix ColorMatrix colorMatrix = new
ColorMatrix( new
float [][] { new
float [] {.3f, .3f, .3f, 0, 0}, new
float [] {.59f, .59f, .59f, 0, 0}, new
float [] {.11f, .11f, .11f, 0, 0}, new
float [] {0, 0, 0, 1, 0}, new
float [] {0, 0, 0, 0, 1} }); //create some image attributes ImageAttributes attributes = new
ImageAttributes(); //set the color matrix attribute attributes.SetColorMatrix(colorMatrix); //draw the original image on the new image //using the grayscale color matrix g.DrawImage(original, new
Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); //dispose the Graphics object g.Dispose(); return
newBitmap; } #endregion #region 增加或減少亮度 /// <summary> /// 增加或減少亮度 /// </summary> /// <param name="img">System.Drawing.Image Source </param> /// <param name="valBrightness">0~255</param> /// <returns></returns> public
System.Drawing.Bitmap AdjustBrightness(System.Drawing.Image img, int
valBrightness) { // 讀入欲轉換的圖片並轉成為 Bitmap System.Drawing.Bitmap bitmap = new
System.Drawing.Bitmap(img); for
( int y = 0; y < bitmap.Height; y++) { for
( int x = 0; x < bitmap.Width; x++) { // 取得每一個 pixel var
pixel = bitmap.GetPixel(x, y); // 判斷 如果處理過後 255 就設定為 255 如果小於則設定為 0 var
pR = ((pixel.R + valBrightness > 255) ? 255 : pixel.R + valBrightness) < 0 ? 0 : ((pixel.R + valBrightness > 255) ? 255 : pixel.R + valBrightness); var
pG = ((pixel.G + valBrightness > 255) ? 255 : pixel.G + valBrightness) < 0 ? 0 : ((pixel.G + valBrightness > 255) ? 255 : pixel.G + valBrightness); var
pB = ((pixel.B + valBrightness > 255) ? 255 : pixel.B + valBrightness) < 0 ? 0 : ((pixel.B + valBrightness > 255) ? 255 : pixel.B + valBrightness); // 將改過的 RGB 寫回 System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixel.A, pR, pG, pB); bitmap.SetPixel(x, y, newColor); } } // 回傳結果 return
bitmap; } #endregion #region 浮雕效果 /// <summary> /// 浮雕效果 /// </summary> /// <param name="src">一个图片实例</param> /// <returns></returns> public
Bitmap AdjustToStone(Bitmap src) { // 依照 Format24bppRgb 每三个表示一 Pixel 0: 蓝 1: 绿 2: 红 BitmapData bitmapData = src.LockBits( new
Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); unsafe { // 抓住第一个 Pixel 第一个数值 byte * p = ( byte *)( void *)bitmapData.Scan0; // 跨步值 - 宽度 *3 可以算出畸零地 之后跳到下一行 int
nOffset = bitmapData.Stride - src.Width * 3; for
( int y = 0; y < src.Height; ++y) { for
( int x = 0; x < src.Width; ++x) { // 为了理解方便 所以特地在命名 int
r, g, b; // 先取得下一个 Pixel var
q = p + 3; r = Math.Abs(p[2] - q[2] + 128); r = r < 0 ? 0 : r; r = r > 255 ? 255 : r; p[2] = ( byte )r; g = Math.Abs(p[1] - q[1] + 128); g = g < 0 ? 0 : g; g = g > 255 ? 255 : g; p[1] = ( byte )g; b = Math.Abs(p[0] - q[0] + 128); b = b < 0 ? 0 : b; b = b > 255 ? 255 : b; p[0] = ( byte )b; // 跳去下一个 Pixel p += 3; } // 跨越畸零地 p += nOffset; } } src.UnlockBits(bitmapData); return
src; } #endregion #region 水波纹效果 /// <summary> /// 水波纹效果 /// </summary> /// <param name="src"></param> /// <param name="nWave">坡度</param> /// www.it165.net /// <returns></returns> public
Bitmap AdjustRippleEffect(Bitmap src, short
nWave) { int
nWidth = src.Width; int
nHeight = src.Height; // 透过公式进行水波纹的採样 PointF[,] fp = new
PointF[nWidth, nHeight]; Point[,] pt = new
Point[nWidth, nHeight]; Point mid = new
Point(); mid.X = nWidth / 2; mid.Y = nHeight / 2; double
newX, newY; double
xo, yo; //先取样将水波纹座标跟RGB取出 for
( int x = 0; x < nWidth; ++x) for
( int y = 0; y < nHeight; ++y) { xo = (( double )nWave * Math.Sin(2.0 * 3.1415 * ( float )y / 128.0)); yo = (( double )nWave * Math.Cos(2.0 * 3.1415 * ( float )x / 128.0)); newX = (x + xo); newY = (y + yo); if
(newX > 0 && newX < nWidth) { fp[x, y].X = ( float )newX; pt[x, y].X = ( int )newX; } else { fp[x, y].X = ( float )0.0; pt[x, y].X = 0; } if
(newY > 0 && newY < nHeight) { fp[x, y].Y = ( float )newY; pt[x, y].Y = ( int )newY; } else { fp[x, y].Y = ( float )0.0; pt[x, y].Y = 0; } } //进行合成 Bitmap bSrc = (Bitmap)src.Clone(); // 依照 Format24bppRgb 每三个表示一 Pixel 0: 蓝 1: 绿 2: 红 BitmapData bitmapData = src.LockBits( new
Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); BitmapData bmSrc = bSrc.LockBits( new
Rectangle(0, 0, bSrc.Width, bSrc.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int
scanline = bitmapData.Stride; IntPtr Scan0 = bitmapData.Scan0; IntPtr SrcScan0 = bmSrc.Scan0; unsafe { byte * p = ( byte *)( void *)Scan0; byte * pSrc = ( byte *)( void *)SrcScan0; int
nOffset = bitmapData.Stride - src.Width * 3; int
xOffset, yOffset; for
( int y = 0; y < nHeight; ++y) { for
( int x = 0; x < nWidth; ++x) { xOffset = pt[x, y].X; yOffset = pt[x, y].Y; if
(yOffset >= 0 && yOffset < nHeight && xOffset >= 0 && xOffset < nWidth) { p[0] = pSrc[(yOffset * scanline) + (xOffset * 3)]; p[1] = pSrc[(yOffset * scanline) + (xOffset * 3) + 1]; p[2] = pSrc[(yOffset * scanline) + (xOffset * 3) + 2]; } p += 3; } p += nOffset; } } src.UnlockBits(bitmapData); bSrc.UnlockBits(bmSrc); return
src; } #endregion #region 调整曝光度值 /// <summary> /// 调整曝光度值 /// </summary> /// <param name="src">原图</param> /// <param name="r"></param> /// <param name="g"></param> /// <param name="b"></param> /// <returns></returns> public
Bitmap AdjustGamma(Bitmap src, double
r, double
g, double
b) { // 判断是不是在0.2~5 之间 r = Math.Min(Math.Max(0.2, r), 5); g = Math.Min(Math.Max(0.2, g), 5); b = Math.Min(Math.Max(0.2, b), 5); // 依照 Format24bppRgb 每三个表示一 Pixel 0: 蓝 1: 绿 2: 红 BitmapData bitmapData = src.LockBits( new
Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); unsafe { // 抓住第一个 Pixel 第一个数值 byte * p = ( byte *)( void *)bitmapData.Scan0; // 跨步值 - 宽度 *3 可以算出畸零地 之后跳到下一行 int
nOffset = bitmapData.Stride - src.Width * 3; for
( int y = 0; y < src.Height; y++) { for
( int x = 0; x < src.Width; x++) { p[2] = ( byte )Math.Min(255, ( int )((255.0 * Math.Pow(p[2] / 255.0, 1.0 / r)) + 0.5)); p[1] = ( byte )Math.Min(255, ( int )((255.0 * Math.Pow(p[1] / 255.0, 1.0 / g)) + 0.5)); p[0] = ( byte )Math.Min(255, ( int )((255.0 * Math.Pow(p[0] / 255.0, 1.0 / b)) + 0.5)); // 跳去下一个 Pixel p += 3; } // 跨越畸零地 p += nOffset; } } src.UnlockBits(bitmapData); return
src; } #endregion #region 高对比,对过深的颜色调浅,过浅的颜色调深。 /// <summary> /// 高对比,对过深的颜色调浅,过浅的颜色调深。 /// </summary> /// <param name="src"></param> /// <param name="effectThreshold"> 高对比程度 -100~100</param> /// <returns></returns> public
Bitmap Contrast(Bitmap src, float
effectThreshold) { // 依照 Format24bppRgb 每三个表示一 Pixel 0: 蓝 1: 绿 2: 红 BitmapData bitmapData = src.LockBits( new
Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); // 判断是否在 -100~100 effectThreshold = effectThreshold < -100 ? -100 : effectThreshold; effectThreshold = effectThreshold > 100 ? 100 : effectThreshold; effectThreshold = ( float )((100.0 + effectThreshold) / 100.0); effectThreshold *= effectThreshold; unsafe { // 抓住第一个 Pixel 第一个数值 www.it165.net byte * p = ( byte *)( void *)bitmapData.Scan0; // 跨步值 - 宽度 *3 可以算出畸零地 之后跳到下一行 int
nOffset = bitmapData.Stride - src.Width * 3; for
( int y = 0; y < src.Height; y++) { for
( int x = 0; x < src.Width; x++) { double
buffer = 0; // 公式 (Red/255)-0.5= 偏离中间值程度 // ((偏离中间值程度 * 影响范围)+0.4 ) * 255 buffer = ((((p[2] / 255.0) - 0.5) * effectThreshold) + 0.5) * 255.0; buffer = buffer > 255 ? 255 : buffer; buffer = buffer < 0 ? 0 : buffer; p[2] = ( byte )buffer; buffer = ((((p[1] / 255.0) - 0.5) * effectThreshold) + 0.5) * 255.0; buffer = buffer > 255 ? 255 : buffer; buffer = buffer < 0 ? 0 : buffer; p[1] = ( byte )buffer; buffer = ((((p[0] / 255.0) - 0.5) * effectThreshold) + 0.5) * 255.0; buffer = buffer > 255 ? 255 : buffer; buffer = buffer < 0 ? 0 : buffer; p[0] = ( byte )buffer; // 跳去下一个 Pixel p += 3; } // 跨越畸零地 p += nOffset; } } src.UnlockBits(bitmapData); return
src; } #endregion #region 对图片进行雾化效果 /// <summary> /// 对图片进行雾化效果 /// </summary> /// <param name="bmp"></param> /// <returns></returns> public
Bitmap Atomization(Bitmap bmp) { int
Height = bmp.Height; int
Width = bmp.Width; Bitmap newBitmap = new
Bitmap(Width, Height); Bitmap oldBitmap = bmp; Color pixel; for
( int x = 1; x < Width - 1; x++) { for
( int y = 1; y < Height - 1; y++) { Random MyRandom = new
Random( Guid.NewGuid().GetHashCode()); int
k = MyRandom.Next(123456); //像素块大小 int
dx = x + k % 19; int
dy = y + k % 19; if
(dx >= Width) dx = Width - 1; if
(dy >= Height) dy = Height - 1; pixel = oldBitmap.GetPixel(dx, dy); newBitmap.SetPixel(x, y, pixel); } } return
newBitmap; } #endregion } //END Class DrawValidationCode #endregion #region 高斯模糊算法 /// <summary> /// 高斯模糊算法 /// </summary> public
class Gaussian { public
static double [,] Calculate1DSampleKernel( double
deviation, int
size) { double [,] ret = new
double [size, 1]; double
sum = 0; int
half = size / 2; for
( int i = 0; i < size; i++) { ret[i, 0] = 1 / (Math.Sqrt(2 * Math.PI) * deviation) * Math.Exp(-(i - half) * (i - half) / (2 * deviation * deviation)); sum += ret[i, 0]; } return
ret; } public
static double [,] Calculate1DSampleKernel( double
deviation) { int
size = ( int )Math.Ceiling(deviation * 3) * 2 + 1; return
Calculate1DSampleKernel(deviation, size); } public
static double [,] CalculateNormalized1DSampleKernel( double
deviation) { return
NormalizeMatrix(Calculate1DSampleKernel(deviation)); } public
static double [,] NormalizeMatrix( double [,] matrix) { double [,] ret = new
double [matrix.GetLength(0), matrix.GetLength(1)]; double
sum = 0; for
( int i = 0; i < ret.GetLength(0); i++) { for
( int j = 0; j < ret.GetLength(1); j++) sum += matrix[i, j]; } if
(sum != 0) { for
( int i = 0; i < ret.GetLength(0); i++) { for
( int j = 0; j < ret.GetLength(1); j++) ret[i, j] = matrix[i, j] / sum; } } return
ret; } public
static double [,] GaussianConvolution( double [,] matrix, double
deviation) { double [,] kernel = CalculateNormalized1DSampleKernel(deviation); double [,] res1 = new
double [matrix.GetLength(0), matrix.GetLength(1)]; double [,] res2 = new
double [matrix.GetLength(0), matrix.GetLength(1)]; //x-direction for
( int i = 0; i < matrix.GetLength(0); i++) { for
( int j = 0; j < matrix.GetLength(1); j++) res1[i, j] = processPoint(matrix, i, j, kernel, 0); } //y-direction for
( int i = 0; i < matrix.GetLength(0); i++) { for
( int j = 0; j < matrix.GetLength(1); j++) res2[i, j] = processPoint(res1, i, j, kernel, 1); } return
res2; } private
static double processPoint( double [,] matrix, int
x, int
y, double [,] kernel, int
direction) { double
res = 0; int
half = kernel.GetLength(0) / 2; for
( int i = 0; i < kernel.GetLength(0); i++) { int
cox = direction == 0 ? x + i - half : x; int
coy = direction == 1 ? y + i - half : y; if
(cox >= 0 && cox < matrix.GetLength(0) && coy >= 0 && coy < matrix.GetLength(1)) { res += matrix[cox, coy] * kernel[i, 0]; } } return
res; } /// <summary> /// 对颜色值进行灰色处理 /// </summary> /// <param name="cr"></param> /// <returns></returns> private
Color grayscale(Color cr) { return
Color.FromArgb(cr.A, ( int )(cr.R * .3 + cr.G * .59 + cr.B * 0.11), ( int )(cr.R * .3 + cr.G * .59 + cr.B * 0.11), ( int )(cr.R * .3 + cr.G * .59 + cr.B * 0.11)); } /// <summary> /// 对图片进行高斯模糊 /// </summary> /// <param name="d">模糊数值,数值越大模糊越很</param> /// <param name="image">一个需要处理的图片</param> /// <returns></returns> public
Bitmap FilterProcessImage( double
d, Bitmap image) { Bitmap ret = new
Bitmap(image.Width, image.Height); Double[,] matrixR = new
Double[image.Width, image.Height]; Double[,] matrixG = new
Double[image.Width, image.Height]; Double[,] matrixB = new
Double[image.Width, image.Height]; for
( int i = 0; i < image.Width; i++) { for
( int j = 0; j < image.Height; j++) { //matrix[i, j] = grayscale(image.GetPixel(i, j)).R; matrixR[i, j] = image.GetPixel(i, j).R; matrixG[i, j] = image.GetPixel(i, j).G; matrixB[i, j] = image.GetPixel(i, j).B; } } matrixR = Gaussian.GaussianConvolution(matrixR, d); matrixG = Gaussian.GaussianConvolution(matrixG, d); matrixB = Gaussian.GaussianConvolution(matrixB, d); for
( int i = 0; i < image.Width; i++) { for
( int j = 0; j < image.Height; j++) { Int32 R = ( int )Math.Min(255, matrixR[i, j]); Int32 G = ( int )Math.Min(255, matrixG[i, j]); Int32 B = ( int )Math.Min(255, matrixB[i, j]); ret.SetPixel(i, j, Color.FromArgb(R, G, B)); } } return
ret; } } #endregion } |
C# :GDI+技术生成复杂型彩色验证码(转载),布布扣,bubuko.com
原文:http://www.cnblogs.com/Fooo/p/3576800.html