Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Drawing;
- using System.Windows.Forms;
- using System.Drawing.Drawing2D;
- class CaptchaBox : PictureBox
- {
- #region Properties
- private Color _textcolor = Color.Black;
- public Color TextColor
- {
- get { return _textcolor; }
- set { _textcolor = value; Invalidate(); }
- }
- private Font _font = new Font("Segoe UI", 20);
- public override Font Font
- {
- get { return _font; }
- set { _font = value; Invalidate(); }
- }
- private string _texttouse = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz!#$%^&*()1234567890";
- public string RandomCharacters
- {
- get { return _texttouse; }
- set { _texttouse = value; Invalidate(); }
- }
- private int _captchalength = 8;
- public int CaptchaTextLength
- {
- get { return _captchalength; }
- set { _captchalength = value; Invalidate(); }
- }
- private int _numberoflines = 50;
- public int NumberOfLines
- {
- get { return _numberoflines; }
- set { _numberoflines = value; Invalidate(); }
- }
- #endregion
- #region Events
- public CaptchaBox()
- {
- SetStyle(ControlStyles.AllPaintingInWmPaint, true);
- SetStyle(ControlStyles.ResizeRedraw, true);
- SetStyle(ControlStyles.UserPaint, true);
- SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
- SetStyle(ControlStyles.SupportsTransparentBackColor, true);
- }
- protected override void CreateHandle()
- {
- base.CreateHandle();
- this.Size = new Size(241, 69);
- this.BorderStyle = BorderStyle.FixedSingle;
- }
- public string CaptchaText = string.Empty;
- private Random rnd = new Random();
- protected override void OnPaint(PaintEventArgs e)
- {
- Bitmap B = new Bitmap(Width, Height);
- Graphics G = Graphics.FromImage(B);
- Rectangle BoxSize = new Rectangle(0, 0, this.Width, this.Height);
- Brush txtcolor = new SolidBrush(Color.FromArgb(rnd.Next(160, 255), _textcolor));
- G.SmoothingMode = SmoothingMode.HighQuality;
- G.Clear(BackColor);
- for (int I = 0; I < _numberoflines; I++)
- {
- 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));
- }
- StringFormat SFormat = new StringFormat();
- SFormat.Alignment = StringAlignment.Center;
- SFormat.LineAlignment = StringAlignment.Center;
- G.RotateTransform(rnd.Next(-7, 7), MatrixOrder.Append);
- string randomstr = CreateRandomString();
- G.DrawString(randomstr, Font, txtcolor, BoxSize, SFormat);
- CaptchaText = randomstr;
- e.Graphics.DrawImage(B, 0, 0);
- G.Dispose(); B.Dispose();
- }
- #endregion
- #region Create Randoms
- private Color CreateRandomColor()
- {
- return Color.FromArgb(100, rnd.Next(255), rnd.Next(255), rnd.Next(255));
- }
- private string CreateRandomString()
- {
- char[] buffer = new char[_captchalength];
- for (int i = 0; i < _captchalength; i++)
- {
- if (_texttouse == string.Empty)
- {
- string newtext = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz1234567890";
- buffer[i] = newtext[rnd.Next(newtext.Length)];
- }
- else
- {
- buffer[i] = _texttouse[rnd.Next(_texttouse.Length)];
- }
- }
- return new string(buffer);
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment