生成文字扭曲的验证码(自定义扭曲程度)

来源:互联网 发布:软件小冰读心术 编辑:程序博客网 时间:2024/05/01 07:19
public partial class Include_VisualVerification : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        CS.Common.VisualImage vi = new CS.Common.VisualImage();        Response.ClearContent();        Response.ContentType = "image/JPEG";        if (Session["VisualVerification"] == null)            Session.Add("VisualVerification", string.Empty);        string vcode = (string)Session["VisualVerification"];        Response.BinaryWrite(vi.GeneralImage(40, 4, out vcode));        Session["VisualVerification"] = vcode;    }}

 

#region VisualImage 验证码    public class VisualImage    {        /// <summary>        /// 产生扭曲效果的随机文本图象JPEG        /// </summary>        /// <param name="length">随机文本长度</param>        /// <param name="strRnd">随机产生的字符串</param>        /// <returns>JPEG格式的文件流</returns>        public byte[] GeneralImage(int fontSize, int length, out string strRnd)        {            int bmpWidth = fontSize * length + 10;            int bmpHeigh = fontSize + 30;            Bitmap bmp = new Bitmap(bmpWidth, bmpHeigh);            Font font = new Font("Courier New", fontSize, FontStyle.Bold);            Graphics g = Graphics.FromImage(bmp);            g.FillRectangle(Brushes.White, 0, 0, bmpWidth, bmpHeigh); // 填充背景色, 否则默认为 black            strRnd = GeneralString(length);            g.DrawString(strRnd, font, Brushes.Black, 10, 10);            Random random = new Random();            bmp = TwistImage(bmp, true, random.Next(10, 23), random.Next(1, 8));//22.33, 0.03);            System.IO.MemoryStream bstream = new System.IO.MemoryStream();            bmp.Save(bstream, System.Drawing.Imaging.ImageFormat.Jpeg);            bmp.Dispose();            g.Dispose();            font.Dispose();            byte[] byteReturn = bstream.ToArray();            bstream.Close();            return byteReturn;        }        public static string GeneralString(int length)        {            System.Text.StringBuilder sbRnd = new System.Text.StringBuilder();            Random random = new Random();            for (int i = 0; i < length; ++i)            {                sbRnd.Append(Convert.ToChar(random.Next(97, 122)));            }            return sbRnd.ToString();        }        /// <summary>        /// 正弦曲线Wave扭曲图片        /// </summary>        /// <param name="srcBmp"></param>        /// <param name="bXDir"></param>        /// <param name="nMultValue">波形的幅度倍数</param>        /// <param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param>        /// <returns></returns>        private Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)        {            double PI = 6.283185307179586476925286766559;            Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);            // 将位图背景填充为白色            Graphics graph = Graphics.FromImage(destBmp);            graph.FillRectangle(new SolidBrush(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 ? (PI * (double)j) / dBaseAxisLen : (PI * (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);                    Color color = srcBmp.GetPixel(i, j);                    if (nOldX >= 0 && nOldX < destBmp.Width                     && nOldY >= 0 && nOldY < destBmp.Height)                    {                        destBmp.SetPixel(nOldX, nOldY, color);                    }                }            }            srcBmp.Dispose();            return destBmp;        }    }    #endregion