Ethernal_Five

[C#] Captcha Generator

Feb 23rd, 2012
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.61 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.Drawing.Drawing2D;
  6.  
  7. class CaptchaBox : PictureBox
  8. {
  9.     #region Properties
  10.     private Color _textcolor = Color.Black;
  11.     public Color TextColor
  12.     {
  13.         get { return _textcolor; }
  14.         set { _textcolor = value; Invalidate(); }
  15.     }
  16.  
  17.     private Font _font = new Font("Segoe UI", 20);
  18.     public override Font Font
  19.     {
  20.         get { return _font; }
  21.         set { _font = value; Invalidate(); }
  22.     }
  23.  
  24.     private string _texttouse = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz!#$%^&*()1234567890";
  25.     public string RandomCharacters
  26.     {
  27.         get { return _texttouse; }
  28.         set { _texttouse = value; Invalidate(); }
  29.     }
  30.  
  31.     private int _captchalength = 8;
  32.     public int CaptchaTextLength
  33.     {
  34.         get { return _captchalength; }
  35.         set { _captchalength = value; Invalidate(); }
  36.     }
  37.  
  38.     private int _numberoflines = 50;
  39.     public int NumberOfLines
  40.     {
  41.         get { return _numberoflines; }
  42.         set { _numberoflines = value; Invalidate(); }
  43.     }
  44.     #endregion
  45.  
  46.     #region Events
  47.     public CaptchaBox()
  48.     {
  49.         SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  50.         SetStyle(ControlStyles.ResizeRedraw, true);
  51.         SetStyle(ControlStyles.UserPaint, true);
  52.         SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  53.         SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  54.     }
  55.  
  56.     protected override void CreateHandle()
  57.     {
  58.         base.CreateHandle();
  59.         this.Size = new Size(241, 69);
  60.         this.BorderStyle = BorderStyle.FixedSingle;
  61.     }
  62.  
  63.     public string CaptchaText = string.Empty;
  64.     private Random rnd = new Random();
  65.     protected override void OnPaint(PaintEventArgs e)
  66.     {
  67.         Bitmap B = new Bitmap(Width, Height);
  68.         Graphics G = Graphics.FromImage(B);
  69.  
  70.         Rectangle BoxSize = new Rectangle(0, 0, this.Width, this.Height);
  71.         Brush txtcolor = new SolidBrush(Color.FromArgb(rnd.Next(160, 255), _textcolor));
  72.         G.SmoothingMode = SmoothingMode.HighQuality;
  73.  
  74.         G.Clear(BackColor);
  75.  
  76.         for (int I = 0; I < _numberoflines; I++)
  77.         {
  78.             G.DrawLine(new Pen(Color.FromArgb(rnd.Next(128, 255),CreateRandomColor())), rnd.Next(0, BoxSize.Width), rnd.Next(0, BoxSize.Height), rnd.Next(BoxSize.Width), rnd.Next(BoxSize.Height));
  79.         }
  80.  
  81.         StringFormat SFormat = new StringFormat();
  82.         SFormat.Alignment = StringAlignment.Center;
  83.         SFormat.LineAlignment = StringAlignment.Center;
  84.  
  85.         G.RotateTransform(rnd.Next(-7, 7), MatrixOrder.Append);
  86.  
  87.         string randomstr = CreateRandomString();
  88.         G.DrawString(randomstr, Font, txtcolor, BoxSize, SFormat);
  89.         CaptchaText = randomstr;
  90.  
  91.         e.Graphics.DrawImage(B, 0, 0);
  92.         G.Dispose(); B.Dispose();
  93.     }
  94.     #endregion
  95.  
  96.     #region Create Randoms
  97.     private Color CreateRandomColor()
  98.     {
  99.         return Color.FromArgb(100, rnd.Next(255), rnd.Next(255), rnd.Next(255));
  100.     }
  101.  
  102.     private string CreateRandomString()
  103.     {
  104.         char[] buffer = new char[_captchalength];
  105.         for (int i = 0; i < _captchalength; i++)
  106.         {
  107.             if (_texttouse == string.Empty)
  108.             {
  109.                 string newtext = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz1234567890";
  110.                 buffer[i] = newtext[rnd.Next(newtext.Length)];
  111.             }
  112.             else
  113.             {
  114.                 buffer[i] = _texttouse[rnd.Next(_texttouse.Length)];
  115.             }
  116.         }
  117.         return new string(buffer);
  118.     }
  119.     #endregion
  120. }
Advertisement
Add Comment
Please, Sign In to add comment