| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- //############################################################
- // https://github.com/yuzhengyang
- // author:yuzhengyang
- //############################################################
- using System;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Text;
- namespace Azylee.Core.IOUtils.ImageUtils
- {
- /// <summary>
- /// Captcha
- /// </summary>
- public class CaptchaHelper
- {
- #region Private Field
- /// <summary>
- /// 随机种子
- /// </summary>
- private Random objRandom = new Random();
- #endregion
- #region Public Property
- #region 验证码长度
- /// <summary>
- /// 验证码长度
- /// </summary>
- private int length = 4;
- /// <summary>
- /// 验证码长度(默认为4)
- /// </summary>
- public int Length
- {
- get { return this.length; }
- set { this.length = value; }
- }
- #endregion
- #region 验证码字符串
- /// <summary>
- /// 验证码字符串
- /// </summary>
- private string verifyCodeText = null;
- /// <summary>
- /// 验证码字符串
- /// </summary>
- public string VerifyCodeText
- {
- get { return this.verifyCodeText; }
- set { this.verifyCodeText = value; }
- }
- #endregion
- #region 是否加入小写字母
- /// <summary>
- /// 是否加入小写字母
- /// </summary>
- private bool addLowerLetter = false;
- /// <summary>
- /// 是否加入小写字母(不包括o)
- /// </summary>
- public bool AddLowerLetter
- {
- get { return this.addLowerLetter; }
- set { this.addLowerLetter = value; }
- }
- #endregion
- #region 是否加入大写字母
- /// <summary>
- /// 是否加入大写字母
- /// </summary>
- private bool addUpperLetter = false;
- /// <summary>
- /// 是否加入大写字母(不包括O)
- /// </summary>
- public bool AddUpperLetter
- {
- get { return this.addUpperLetter; }
- set { this.addUpperLetter = value; }
- }
- #endregion
- #region 字体大小
- /// <summary>
- /// 字体大小
- /// </summary>
- private int fontSize = 18;
- /// <summary>
- /// 字体大小(默认为18)
- /// </summary>
- public int FontSize
- {
- get { return this.fontSize; }
- set { this.fontSize = value; }
- }
- #endregion
- #region 字体颜色
- /// <summary>
- /// 字体颜色
- /// </summary>
- private Color fontColor = Color.Blue;
- /// <summary>
- /// 字体颜色(默认为Blue)
- /// </summary>
- public Color FontColor
- {
- get { return this.fontColor; }
- set { this.fontColor = value; }
- }
- #endregion
- #region 字体类型
- /// <summary>
- /// 字体类型
- /// </summary>
- private string fontFamily = "Verdana";
- /// <summary>
- /// 字体类型(默认为Verdana)
- /// </summary>
- public string FontFamily
- {
- get { return this.fontFamily; }
- set { this.fontFamily = value; }
- }
- #endregion
- #region 背景色
- /// <summary>
- /// 背景色
- /// </summary>
- private Color backgroundColor = Color.AliceBlue;
- /// <summary>
- /// 背景色(默认为AliceBlue)
- /// </summary>
- public Color BackgroundColor
- {
- get { return this.backgroundColor; }
- set { this.backgroundColor = value; }
- }
- #endregion
- #region 前景噪点数量
- /// <summary>
- /// 前景噪点数量
- /// </summary>
- private int foreNoisePointCount = 2;
- /// <summary>
- /// 前景噪点数量(默认为2)
- /// </summary>
- public int ForeNoisePointCount
- {
- get { return this.foreNoisePointCount; }
- set { this.foreNoisePointCount = value; }
- }
- #endregion
- #region 随机码的旋转角度
- /// <summary>
- /// 随机码的旋转角度
- /// </summary>
- private int randomAngle = 45;
- /// <summary>
- /// 随机码的旋转角度(默认为40度)
- /// </summary>
- public int RandomAngle
- {
- get { return this.randomAngle; }
- set { this.randomAngle = value; }
- }
- #endregion
- #endregion
- #region Constructor Method
- /// <summary>
- /// 构造方法
- /// </summary>
- public CaptchaHelper()
- {
- this.GetText();
- }
- #endregion
- #region Private Method
- /// <summary>
- /// 得到验证码字符串
- /// </summary>
- private void GetText()
- {
- //没有外部输入验证码时随机生成
- if (String.IsNullOrEmpty(this.verifyCodeText))
- {
- StringBuilder objStringBuilder = new StringBuilder();
- //加入数字1-9
- for (int i = 1; i <= 9; i++)
- {
- objStringBuilder.Append(i.ToString());
- }
- //加入大写字母A-Z,不包括O
- if (this.addUpperLetter)
- {
- char temp = ' ';
- for (int i = 0; i < 26; i++)
- {
- temp = Convert.ToChar(i + 65);
- //如果生成的字母不是'O'
- if (!temp.Equals('O'))
- {
- objStringBuilder.Append(temp);
- }
- }
- }
- //加入小写字母a-z,不包括o
- if (this.addLowerLetter)
- {
- char temp = ' ';
- for (int i = 0; i < 26; i++)
- {
- temp = Convert.ToChar(i + 97);
- //如果生成的字母不是'o'
- if (!temp.Equals('o'))
- {
- objStringBuilder.Append(temp);
- }
- }
- }
- //生成验证码字符串
- {
- int index = 0;
- for (int i = 0; i < length; i++)
- {
- index = objRandom.Next(0, objStringBuilder.Length);
- this.verifyCodeText += objStringBuilder[index];
- objStringBuilder.Remove(index, 1);
- }
- }
- }
- }
- /// <summary>
- /// 得到验证码图片
- /// </summary>
- private Bitmap GetImage()
- {
- Bitmap result = null;
- //创建绘图
- result = new Bitmap(this.verifyCodeText.Length * 16, 25);
- using (Graphics objGraphics = Graphics.FromImage(result))
- {
- objGraphics.SmoothingMode = SmoothingMode.HighQuality;
- //清除整个绘图面并以指定背景色填充
- objGraphics.Clear(this.backgroundColor);
- //创建画笔
- using (SolidBrush objSolidBrush = new SolidBrush(this.fontColor))
- {
- this.AddForeNoisePoint(result);
- this.AddBackgroundNoisePoint(result, objGraphics);
- //文字居中
- StringFormat objStringFormat = new StringFormat(StringFormatFlags.NoClip);
- objStringFormat.Alignment = StringAlignment.Center;
- objStringFormat.LineAlignment = StringAlignment.Center;
- //字体样式
- Font objFont = new Font(this.fontFamily, objRandom.Next(this.fontSize - 3, this.fontSize), FontStyle.Regular);
- //验证码旋转,防止机器识别
- char[] chars = this.verifyCodeText.ToCharArray();
- for (int i = 0; i < chars.Length; i++)
- {
- //转动的度数
- float angle = objRandom.Next(-this.randomAngle, this.randomAngle);
- objGraphics.TranslateTransform(12, 12);
- objGraphics.RotateTransform(angle);
- objGraphics.DrawString(chars[i].ToString(), objFont, objSolidBrush, -2, 2, objStringFormat);
- objGraphics.RotateTransform(-angle);
- objGraphics.TranslateTransform(2, -12);
- }
- }
- }
- return result;
- }
- /// <summary>
- /// 添加前景噪点
- /// </summary>
- /// <param name="objBitmap"></param>
- private void AddForeNoisePoint(Bitmap objBitmap)
- {
- for (int i = 0; i < objBitmap.Width * this.foreNoisePointCount; i++)
- {
- objBitmap.SetPixel(objRandom.Next(objBitmap.Width), objRandom.Next(objBitmap.Height), this.fontColor);
- }
- }
- /// <summary>
- /// 添加背景噪点
- /// </summary>
- /// <param name="objBitmap"></param>
- /// <param name="objGraphics"></param>
- private void AddBackgroundNoisePoint(Bitmap objBitmap, Graphics objGraphics)
- {
- using (Pen objPen = new Pen(Color.Azure, 0))
- {
- for (int i = 0; i < objBitmap.Width * 2; i++)
- {
- objGraphics.DrawRectangle(objPen, objRandom.Next(objBitmap.Width), objRandom.Next(objBitmap.Height), 1, 1);
- }
- }
- }
- #endregion
- #region Public Method
- //public void Output(HttpResponse objHttpResponse)
- //{
- // using (Bitmap objBitmap = this.GetImage())
- // {
- // if (objBitmap != null)
- // {
- // using (MemoryStream objMS = new MemoryStream())
- // {
- // objBitmap.Save(objMS, ImageFormat.Jpeg);
- // HttpContext.Current.Response.ClearContent();
- // HttpContext.Current.Response.ContentType = "image/Jpeg";
- // HttpContext.Current.Response.BinaryWrite(objMS.ToArray());
- // HttpContext.Current.Response.Flush();
- // HttpContext.Current.Response.End();
- // }
- // }
- // }
- //}
- public void Output(string savePath)
- {
- using (Bitmap objBitmap = this.GetImage())
- {
- if (objBitmap != null)
- {
- objBitmap.Save(savePath, ImageFormat.Jpeg);
- }
- }
- }
- #endregion
- }
- }
|