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
| using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text;
namespace MyWebsite.BLL { public class CaptchaBLL { private const int _imageWidth = 80;
private const int _imageHeight = 30;
private const int _textColorDepth = 80;
private const int _interferenceColorDepth = 200;
private const string _chars = "0123456789";
private readonly static Random _random = new Random();
private readonly static Color _backGroundColor = Color.White;
private readonly static List<Font> _fonts = new string[] { "Arial", "Arial Black", "Calibri", "Cambria", "Verdana", "Trebuchet MS", "Palatino Linotype", "Georgia", "Constantia", "Consolas", "Comic Sans MS", "Century Gothic", "Candara", "Courier New", "Times New Roman" }.Select(f => new Font(f, 18, FontStyle.Bold | FontStyle.Italic)).ToList();
public string ComputeMd5Hash(string input) { var encoding = new ASCIIEncoding(); var bytes = encoding.GetBytes(input); var md5Hasher = MD5.Create(); return BitConverter.ToString(md5Hasher.ComputeHash(bytes)); }
public byte[] GenerateCaptchaImage(string text) { using (var bmpOut = new Bitmap(_imageWidth, _imageHeight)) { float orientationAngle = _random.Next(0, 359); var g = Graphics.FromImage(bmpOut); var gradientBrush = new LinearGradientBrush( new Rectangle(0, 0, _imageWidth, _imageHeight), _backGroundColor, _backGroundColor, orientationAngle ); g.FillRectangle(gradientBrush, 0, 0, _imageWidth, _imageHeight);
int tempRndAngle = 0; for (int i = 0; i < text.Length; i++) { tempRndAngle = _random.Next(-5, 5); g.RotateTransform(tempRndAngle);
g.DrawString( text[i].ToString(), _fonts[_random.Next(0, _fonts.Count)], new SolidBrush(GetRandomColor(_textColorDepth)), i * _imageWidth / (text.Length + 1) * 1.2f, (float)_random.NextDouble() );
g.RotateTransform(-tempRndAngle); }
InterferenceLines(ref g, text.Length * 2);
ArraySegment<byte> bmpBytes; using (var ms = new MemoryStream()) { bmpOut.Save(ms, ImageFormat.Gif); ms.TryGetBuffer(out bmpBytes); bmpOut.Dispose(); ms.Dispose(); }
return bmpBytes.ToArray(); } }
public string GenerateRandomText(int textLength) { var result = new string(Enumerable.Repeat(_chars, textLength) .Select(s => s[_random.Next(s.Length)]).ToArray()); return result.ToUpper(); }
private static void InterferenceLines(ref Graphics g, int lines) { for (var i = 0; i < lines; i++) { var pan = new Pen(GetRandomColor(_interferenceColorDepth)); var points = new Point[_random.Next(2, 5)]; for (int pi = 0; pi < points.Length; pi++) { points[pi] = new Point(_random.Next(0, _imageWidth), _random.Next(0, _imageHeight)); } g.DrawCurve(pan, points); } }
private static Color GetRandomColor(int depth) { int red = _random.Next(depth); int green = _random.Next(depth); int blue = (red + green > 400) ? 0 : 400 - red - green; blue = (blue > depth) ? depth : blue; return Color.FromArgb(red, green, blue); } } }
|