Asp.Net验证码3

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text; public partial class user_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateImage(GetCheckCode(VCODE_LENGTH));
} protected static int VCODE_LENGTH = ; //产生图片 宽度:_WIDTH, 高度:_HEIGHT
private static readonly int _WIDTH = , _HEIGHT = ;
//字体集
private static readonly string[] _FONT_FAMIly = { "Broadway", "Arial", "Arial Black", "Courier New", "Showcard Gothic", "Algerian", "Wide Latin", "Bernard MT Condensed", "Cooper Black" };
//字体大小集
private static readonly int[] _FONT_SIZE = { , , };
//前景字体颜色集
private static readonly Color[] _COLOR_FACE = { Color.FromArgb(, , ), Color.FromArgb(, , ), Color.FromArgb(, , ), Color.FromArgb(, , ) };
//背景颜色集
private static readonly Color[] _COLOR_BACKGROUND = { Color.FromArgb(, , ), Color.FromArgb(, , ), Color.FromArgb(, , ), Color.FromArgb(, , ) };
//文本布局信息
private static StringFormat _DL_FORMAT = new StringFormat(StringFormatFlags.NoClip);
//左右旋转角度
private static readonly int _ANGLE = ; private string GetCheckCode(int codeCount)
{
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -;
Random rand = new Random();
for (int i = ; i < codeCount; i++)
{
if (temp != -)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next();
if (temp == t)
{
return GetCheckCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
} private void CreateImage(string code)
{
_DL_FORMAT.Alignment = StringAlignment.Center;
_DL_FORMAT.LineAlignment = StringAlignment.Center; long tick = DateTime.Now.Ticks;
Random Rnd = new Random((int)(tick & 0xffffffffL) | (int)(tick >> )); using (Bitmap _img = new Bitmap(_WIDTH, _HEIGHT))
{
using (Graphics g = Graphics.FromImage(_img))
{
g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; Point dot = new Point(, ); // 定义一个无干扰线区间和一个起始位置
int nor = Rnd.Next(), rsta = Rnd.Next();
// 绘制干扰正弦曲线 M:曲线平折度, D:Y轴常量 V:X轴焦距
int M = Rnd.Next() + , D = Rnd.Next() + , V = Rnd.Next() + ; int ColorIndex = Rnd.Next(); float Px_x = 0.0F;
float Px_y = Convert.ToSingle(M * Math.Sin(V * Px_x * Math.PI / ) + D);
float Py_x, Py_y; //填充背景
g.Clear(_COLOR_BACKGROUND[Rnd.Next()]); //前景刷子 //背景刷子
using (Brush _BrushFace = new SolidBrush(_COLOR_FACE[ColorIndex]))
{
#region 绘制正弦线
for (int i = ; i < ; i++)
{
//初始化y点
Py_x = Px_x + ;
Py_y = Convert.ToSingle(M * Math.Sin(V * Py_x * Math.PI / ) + D); //确定线条颜色
if (rsta >= i || i > (rsta + nor))
//初始化画笔
using (Pen _pen = new Pen(_BrushFace, 1.5f))
{
//绘制线条
g.DrawLine(_pen, Px_x, Px_y, Py_x, Py_y);
} //交替x,y坐标点
Px_x = Py_x;
Px_y = Py_y;
}
#endregion //初始化光标的开始位置
g.TranslateTransform(-, ); #region 绘制校验码字符串
for (int i = ; i < code.Length; i++)
{
//随机旋转 角度
int angle = Rnd.Next(-_ANGLE, _ANGLE);
//移动光标到指定位置
g.TranslateTransform(dot.X, dot.Y);
//旋转
g.RotateTransform(angle); //初始化字体
using (Font _font = new Font(_FONT_FAMIly[Rnd.Next(, )], _FONT_SIZE[Rnd.Next(, )]))
{
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(, , _WIDTH, _HEIGHT), _COLOR_FACE[ColorIndex], Color.Orange, 1.2f, true); //绘制
g.DrawString(code[i].ToString(), _font, brush, , , _DL_FORMAT);
}
//反转
g.RotateTransform(-angle);
//重新定位光标位置
g.TranslateTransform(-, -dot.Y);
}
#endregion }
} using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
Response.ContentType = "Image/PNG";
Response.Clear();
Response.BufferOutput = true;
_img.Save(ms, ImageFormat.Png);
ms.Flush();
Response.BinaryWrite(ms.GetBuffer());
Response.End();
}
} }
}

验证码效果:

Asp.Net验证码3

还有2种验证码:

http://www.cnblogs.com/webapi/p/5725576.html

http://www.cnblogs.com/webapi/p/5726489.html

上一篇:[解决方案] pythonchallenge level 5


下一篇:Python字典推导式将cookie字符串转化为字典