示范地址
http://www.qol8.com/ValifyCode.aspx觉得有兴趣的可以找我,只有周末上
[email protected]下载地址
http://h.thec.cn/Kissogram/Downloads/ValifyCode.rar
http://www.qol8.com/ValifyCode.aspx觉得有兴趣的可以找我,只有周末上
[email protected]下载地址
http://h.thec.cn/Kissogram/Downloads/ValifyCode.rar
{
using System;
using System.IO;
using System.Web;
using System.Drawing; //GIF验证码类
public class Validate
{
//设置最少4位验证码
private byte TrueValidateCodeCount = 4;
public byte ValidateCodeCount
{
get
{
return TrueValidateCodeCount;
}
set
{
//验证码至少为3位
if (value > 4)
TrueValidateCodeCount = value;
}
}
protected string ValidateCode = "";
//是否消除锯齿
public bool FontTextRenderingHint = false;
//验证码字体
public string ValidateCodeFont = "Arial";
//验证码型号(像素)
public float ValidateCodeSize = 13;
public int ImageHeight = 23;
//定义验证码中所有的字符
public string AllChar = "1,2,3,4,5,6,7,8,9,0,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"; //获得随机四位数
private void CreateValidate()
{
ValidateCode = "";
//将验证码中所有的字符保存在一个字符串数组中
string[] CharArray = AllChar.Split(',');
int Temp = -1;
//生成一个随机对象
Random RandCode = new Random();
//根据验证码的位数循环
for (int i = 0; i < ValidateCodeCount; i++)
{
//主要是防止生成相同的验证码
if (Temp != -1)
{
//加入时间的刻度
RandCode = new Random(i * Temp * ((int)DateTime.Now.Ticks));
}
int t = RandCode.Next(35);
if (Temp == t)
{
//相等的话重新生成
CreateValidate();
}
Temp = t;
ValidateCode += CharArray[Temp];
}
//错误检测,去除超过指定位数的验证码
if (ValidateCode.Length > TrueValidateCodeCount)
ValidateCode = ValidateCode.Remove(TrueValidateCodeCount);
}
//生成一帧的BMP图象
private void CreateImageBmp(out Bitmap ImageFrame)
{
//获得验证码字符
char[] CodeCharArray = ValidateCode.ToCharArray(0, ValidateCodeCount);
//图像的宽度-与验证码的长度成一定比例
int ImageWidth = (int)(TrueValidateCodeCount * ValidateCodeSize * 1.3 + 4);
//创建一个长20,宽iwidth的图像对象
ImageFrame = new Bitmap(ImageWidth, ImageHeight);
//创建一个新绘图对象
Graphics ImageGraphics = Graphics.FromImage(ImageFrame);
//清除背景色,并填充背景色
//Note:Color.Transparent为透明
ImageGraphics.Clear(Color.White);
//绘图用的字体和字号
Font CodeFont = new Font(ValidateCodeFont, ValidateCodeSize, FontStyle.Bold);
//绘图用的刷子大小
Brush ImageBrush = new SolidBrush(Color.Red);
//字体高度计算
int FontHeight = (int)Math.Max(ImageHeight - ValidateCodeSize - 3, 2);
//创建随机对象
Random rand = new Random();
//开始随机安排字符的位置,并画到图像里
for (int i = 0; i < TrueValidateCodeCount; i++)
{
//生成随机点,决定字符串的开始输出范围
int[] FontCoordinate = new int[2];
FontCoordinate[0] = (int)(i * ValidateCodeSize + rand.Next(1)) + 3;
FontCoordinate[1] = rand.Next(FontHeight);
Point FontDrawPoint = new Point(FontCoordinate[0], FontCoordinate[1]);
//消除锯齿操作
if (FontTextRenderingHint)
ImageGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
else
ImageGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
//格式化刷子属性-用指定的刷子、颜色等在指定的范围内画图
ImageGraphics.DrawString(CodeCharArray[i].ToString(), CodeFont, ImageBrush, FontDrawPoint);
}
ImageGraphics.Dispose();
}
//处理生成的BMP
private void DisposeImageBmp(ref Bitmap ImageFrame)
{
//创建绘图对象
Graphics ImageGraphics = Graphics.FromImage(ImageFrame);
//创建铅笔对象
Pen ImagePen = new Pen(Color.Red, 1);
//创建随机对象
Random rand = new Random();
//创建随机点
Point[] RandPoint = new Point[2];
//随机画线
for (int i = 0; i < 15; i++)
{
RandPoint[0] = new Point(rand.Next(ImageFrame.Width), rand.Next(ImageFrame.Height));
RandPoint[1] = new Point(rand.Next(ImageFrame.Width), rand.Next(ImageFrame.Height));
ImageGraphics.DrawLine(ImagePen, RandPoint[0], RandPoint[1]);
}
ImageGraphics.Dispose();
}
//创建GIF动画
private void CreateImageGif()
{
Bitmap ImageFrame;
Kissogram.Drawing.Gif.AnimatedGifEncoder GifPic = new Kissogram.Drawing.Gif.AnimatedGifEncoder();
MemoryStream BmpMemory = new MemoryStream();
GifPic.Start();
//确保视觉残留
GifPic.SetDelay(5);
//-1:no repeat,0:always repeat
GifPic.SetRepeat(0);
for (int i = 0; i < 20; i++)
{
//创建一帧的图像
CreateImageBmp(out ImageFrame);
//生成随机线条
DisposeImageBmp(ref ImageFrame);
//输出绘图,将图像保存到指定的流
ImageFrame.Save(BmpMemory, System.Drawing.Imaging.ImageFormat.Png);
GifPic.AddFrame(Image.FromStream(BmpMemory));
BmpMemory = new MemoryStream();
}
GifPic.OutPut(ref BmpMemory);
HttpContext.Current.Response.ClearContent();
//配置输出类型
HttpContext.Current.Response.ContentType = "image/Gif";
//输出内容
HttpContext.Current.Response.BinaryWrite(BmpMemory.ToArray());
BmpMemory.Close();
BmpMemory.Dispose();
}
//输出验证码
public void OutPutValidate(string ValidateCodeSession)
{
CreateValidate();
CreateImageGif();
//把生成的验证码输入到SESSION
HttpContext.Current.Session[ValidateCodeSession] = ValidateCode;
}
}
}这是源代码,免得大家去反编译了,不过得希望有良心的不要抄了以后说自己写的
每一帧都划了很多线,确保一般的注册机在分解了GIF后也不容易读出来
人有视觉残留,所以读起来不是很困难大家有对单帧处理能让注册机更难读取的办法请联系我
http://www.codeproject.com/dotnet/NGif.asp
上的一个
再进行了下小修改
我试验了一下,为什么无论我在网页上添加什么内容都不能显示出来,只能显示出那个跳动的动画?为什么呀?
这个不是一个网页
这是输出为GIF图片的(image/gif)使用的时候和其他的验证码程序一样,在网页中调用
<image src="validycode.aspx"/>
开源组件
Kissogram.Drawing.Gif.AnimatedGifEncoder GifPic = new Kissogram.Drawing.Gif.AnimatedGifEncoder();出错了
我包里提供的ValidyCode.aspx不是就能用么
Kissogram.Drawing.Gif.AnimatedGifEncoder GifPic = new Kissogram.Drawing.Gif.AnimatedGifEncoder();出错了
------------------------
还是下载我提供的包
DLL里面还有一个命名空间是 Kissogram.Drawing.Gif
你直接拷贝我上面发的代码会缺少这个GIF生成类
Kissogram.Drawing.Gif.AnimatedGifEncoder GifPic = new Kissogram.Drawing.Gif.AnimatedGifEncoder();出错了
------------------------
还是下载我提供的包
DLL里面还有一个命名空间是 Kissogram.Drawing.Gif
你直接拷贝我上面发的代码会缺少这个GIF生成类
-----------------------------------------------------------------
我拷了``可怎么把这个DLL加到项目里去啊
怎么可能
DLL有没有放错阿
string AllChar = "1234567890ABCDEFGHIJKLMNOPQRSTUWXYZ";Random r = new Random();
string result = "";
for (int i=0; i<4; i++)
{
result += AllChar[r.Next(AllChar.Length)];
}
result既是随机了
怎么可能
DLL有没有放错阿可就是说未实例化,我是把你的那个直接放到wwwroot下面````然后运行``有什么要注意的吗?
http://qol8.com/Down/ValifyCode.rar
点这个下载
http://qol8.com/Down/ValifyCode.htm
http://code.google.com/p/awesomecaptcha/
有兴趣的同学可以去download
http://awesomecaptcha.googlecode.com/files/attachments_2011_04_28.zip