12306動態(tài)驗證碼啟發(fā)之ASP.NET實現(xiàn)動態(tài)GIF驗證碼(附源碼)
12306網(wǎng)站推出“彩色動態(tài)驗證碼機制”,新版驗證碼不但經(jīng)常出現(xiàn)字符疊壓,還不停抖動,不少人大呼“看不清”,稱“那個驗證碼,是畢加索的抽象畫么!”鐵總客服則表示:為了能正常購票只能這樣。而多家搶票軟件接近“報廢”,引發(fā)不少網(wǎng)友不滿的吐槽稱“太抽象太藝術(shù)了”。
以前做項目有時候也會用到驗證碼,但基本都是靜態(tài)的,這次也想湊湊12306的熱鬧。閑言少續(xù),切入正題,先上代碼。
實現(xiàn)方法:
public void ShowCode()
{
//對象實例化
Validate GifValidate = new Validate();
#region 對驗證碼進行設(shè)置(不進行設(shè)置時,將以默認值生成)
//驗證碼位數(shù),不小于4位
GifValidate.ValidateCodeCount = 4;
//驗證碼字體型號(默認13)
GifValidate.ValidateCodeSize = 13;
//驗證碼圖片高度,高度越大,字符的上下偏移量就越明顯
GifValidate.ImageHeight = 23;
//驗證碼字符及線條顏色(需要參考顏色類)
GifValidate.DrawColor = System.Drawing.Color.BlueViolet;
//驗證碼字體(需要填寫服務(wù)器安裝的字體)
GifValidate.ValidateCodeFont = "Arial";
//驗證碼字符是否消除鋸齒
GifValidate.FontTextRenderingHint = false;
//定義驗證碼中所有的字符(","分離),似乎暫時不支持中文
GifValidate.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";
#endregion
//輸出圖像(Session名稱)
GifValidate.OutPutValidate("GetCode");
}
調(diào)用主要方法:
public class Validate
{
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";
public Color DrawColor = Color.BlueViolet;
public bool FontTextRenderingHint = false;
public int ImageHeight = 0x17;
private byte TrueValidateCodeCount = 4;
protected string ValidateCode = "";
public string ValidateCodeFont = "Arial";
public float ValidateCodeSize = 13f;
private void CreateImageBmp(out Bitmap ImageFrame)
{
char[] chArray = this.ValidateCode.ToCharArray(0, this.ValidateCodeCount);
int width = (int) (((this.TrueValidateCodeCount * this.ValidateCodeSize) * 1.3) + 4.0);
ImageFrame = new Bitmap(width, this.ImageHeight);
Graphics graphics = Graphics.FromImage(ImageFrame);
graphics.Clear(Color.White);
Font font = new Font(this.ValidateCodeFont, this.ValidateCodeSize, FontStyle.Bold);
Brush brush = new SolidBrush(this.DrawColor);
int maxValue = (int) Math.Max((float) ((this.ImageHeight - this.ValidateCodeSize) - 3f), (float) 2f);
Random random = new Random();
for (int i = 0; i < this.TrueValidateCodeCount; i++)
{
int[] numArray = new int[] { (((int) (i * this.ValidateCodeSize)) + random.Next(1)) + 3, random.Next(maxValue) };
Point point = new Point(numArray[0], numArray[1]);
if (this.FontTextRenderingHint)
{
graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
}
else
{
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
}
graphics.DrawString(chArray[i].ToString(), font, brush, (PointF) point);
}
graphics.Dispose();
}
private void CreateImageGif()
{
AnimatedGifEncoder encoder = new AnimatedGifEncoder();
MemoryStream stream = new MemoryStream();
encoder.Start();
encoder.SetDelay(5);
encoder.SetRepeat(0);
for (int i = 0; i < 10; i++)
{
Bitmap bitmap;
this.CreateImageBmp(out bitmap);
this.DisposeImageBmp(ref bitmap);
bitmap.Save(stream, ImageFormat.Png);
encoder.AddFrame(Image.FromStream(stream));
stream = new MemoryStream();
}
encoder.OutPut(ref stream);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Gif";
HttpContext.Current.Response.BinaryWrite(stream.ToArray());
stream.Close();
stream.Dispose();
}
private void CreateValidate()
{
this.ValidateCode = "";
string[] strArray = this.AllChar.Split(new char[] { ',' });
int index = -1;
Random random = new Random();
for (int i = 0; i < this.ValidateCodeCount; i++)
{
if (index != -1)
{
random = new Random((i * index) * ((int) DateTime.Now.Ticks));
}
int num3 = random.Next(0x23);
if (index == num3)
{
this.CreateValidate();
}
index = num3;
this.ValidateCode = this.ValidateCode + strArray[index];
}
if (this.ValidateCode.Length > this.TrueValidateCodeCount)
{
this.ValidateCode = this.ValidateCode.Remove(this.TrueValidateCodeCount);
}
}
private void DisposeImageBmp(ref Bitmap ImageFrame)
{
Graphics graphics = Graphics.FromImage(ImageFrame);
Pen pen = new Pen(this.DrawColor, 1f);
Random random = new Random();
Point[] pointArray = new Point[2];
for (int i = 0; i < 15; i++)
{
pointArray[0] = new Point(random.Next(ImageFrame.Width), random.Next(ImageFrame.Height));
pointArray[1] = new Point(random.Next(ImageFrame.Width), random.Next(ImageFrame.Height));
graphics.DrawLine(pen, pointArray[0], pointArray[1]);
}
graphics.Dispose();
}
public void OutPutValidate(string ValidateCodeSession)
{
this.CreateValidate();
this.CreateImageGif();
HttpContext.Current.Session[ValidateCodeSession] = this.ValidateCode;
}
public byte ValidateCodeCount
{
get
{
return this.TrueValidateCodeCount;
}
set
{
if (value > 4)
{
this.TrueValidateCodeCount = value;
}
}
}
}
驗證碼效果:
-----下載源碼-----
以上就是實現(xiàn)ASP.NET的全部過程,還附有源碼,希望可以幫到大家更好地了解ASP.NET驗證碼的生成方法。
相關(guān)文章
asp.net中Timer無刷新定時器的實現(xiàn)方法
這篇文章主要介紹了asp.net中Timer無刷新定時器的實現(xiàn)方法,是一個非常具有實用價值的技巧,需要用到Ajax技術(shù),需要的朋友可以參考下2014-08-08
asp.net textbox javascript實現(xiàn)enter與ctrl+enter互換 文本框發(fā)送消息與換行(類似
今天與大家分享一下 asp.net textbox javascript實現(xiàn)enter與ctrl+enter互換 文本框發(fā)送消息與換行(類似于QQ),這個功能到底怎么實現(xiàn)?首先聲明以下幾點2012-01-01
ASP.NET動態(tài)加載用戶控件的實現(xiàn)方法
動態(tài)加載用戶控件的方法,用asp.net的朋友推薦2008-10-10
.NET連接數(shù)據(jù)庫以及基本的增刪改查操作教程
這篇文章主要給大家介紹了關(guān)于.NET連接數(shù)據(jù)庫以及基本的增刪改查操作教程的相關(guān)資料,對于剛?cè)腴T的新手們來說是個很好的入門教程,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。2018-01-01
ASP.NET MVC5網(wǎng)站開發(fā)項目框架(二)
這篇文章主要介紹了ASP.NET MVC5網(wǎng)站開發(fā)項目框架,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-09-09
利用ASP.NET MVC和Bootstrap快速搭建個人博客之后臺dataTable數(shù)據(jù)列表
jQuery dataTables 插件是一個優(yōu)秀的表格插件,應(yīng)用非常廣泛,本文給大家介紹利用ASP.NET MVC和Bootstrap快速搭建個人博客之后臺dataTable數(shù)據(jù)列表,非常不錯,具有參考借鑒價值,感興趣的朋友一起看下吧2016-07-07
ASP.NET Core 1.0實現(xiàn)郵件發(fā)送功能
這篇文章主要為大家詳細介紹了ASP.NET Core 1.0實現(xiàn)郵件發(fā)送功能的相關(guān)資料,需要的朋友可以參考下2016-07-07
gridview和checkboxlist的嵌套相關(guān)應(yīng)用
gridview和checkboxlist的嵌套使用,會有效的提高開發(fā)的效率,不過很多的童鞋們對此還是很陌生的,接下來將幫助童鞋們實現(xiàn)gridview和checkboxlist的嵌套使用,感興趣的朋友可以了解下,或許對你有所幫助2013-02-02

