12306奇葩驗(yàn)證碼引發(fā)思考之C#實(shí)現(xiàn)驗(yàn)證碼程序
近日鐵路訂票網(wǎng)“12306”又出現(xiàn)多道另類考題,竟要訂票者在8個(gè)圖案中“點(diǎn)擊圖中所有美男子”、“請(qǐng)點(diǎn)擊下圖中所有的非智能眼鏡”、“請(qǐng)點(diǎn)擊下圖中所有的博斯普魯斯海峽”,網(wǎng)友吐槽:比高考題還難,到底是什么樣子的,先跟大家分享一下幾個(gè)例子:
哈哈,是有點(diǎn)奇葩的驗(yàn)證碼,怪不得有人會(huì)說(shuō)“媽媽我已經(jīng)找不到回家”,這讓分秒必爭(zhēng)的春運(yùn)網(wǎng)上搶票者瞬間傻眼,九成網(wǎng)友已經(jīng)被打敗……
正巧小編最近也在研究驗(yàn)證碼,參考了許多網(wǎng)上案例,整理了一篇文章特分享給大家。
驗(yàn)證碼的一般編寫思路為:
1.定義驗(yàn)證碼字符長(zhǎng)度;
2.根據(jù)長(zhǎng)度隨機(jī)生成驗(yàn)證碼字符串;
3.將驗(yàn)證碼字符串轉(zhuǎn)換成圖片形式,并在圖片中生成隨機(jī)噪聲點(diǎn)和聲線(對(duì)驗(yàn)證碼進(jìn)行模糊識(shí)別處理);
4.顯示結(jié)果。
///
/// 生成隨機(jī)驗(yàn)證碼
///
/// 驗(yàn)證碼長(zhǎng)度
///
public string CreateIdentifyingCode(int CodeLen)
{
if (CodeLen < 1)
return String.Empty;
int num;
string checkcode = String.Empty;
Random random = new Random();
for (int index = 0; index < CodeLen; index++)
{
num = random.Next();
if (num % 2 == 0)
checkcode += (char)('0' + (char)(num % 10));
else
checkcode += (char)('A' + (char)(num % 26));
}
return checkcode;
}
-------------------------------------------------------------------------------------------------
///
/// 生成驗(yàn)證碼圖片
///
///
///
private void CreateCheckCodeImage(string checkcode)
{
if (checkcode == null || checkcode.Trim() == String.Empty)
return;
//創(chuàng)建圖片大小
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling(checkcode.Length*12.5),22);
//創(chuàng)建畫板
Graphics graphic = Graphics.FromImage(image);
try
{
Random random = new Random();
graphic.Clear(Color.White);
int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
//畫圖片背景噪聲線
for (int index = 0; index < 25; index++)
{
x1 = random.Next(image.Width);
y1 = random.Next(image.Height);
x2 = random.Next(image.Width);
y2 = random.Next(image.Height);
graphic.DrawLine(new Pen(Color.Silver),x1,y1,x2,y2);
}
Font font = new System.Drawing.Font("Helvetica", 12, (FontStyle.Bold |FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),Color.Blue,Color.DarkBlue,1.2f,true);
graphic.DrawString(checkcode,font,brush,2,2);
int x = 0;
int y = 0;
// 畫圖片的前景噪聲點(diǎn)
for (int index = 0; index < 100; index++)
{
x = random.Next(image.Width);
y = random.Next(image.Height);
image.SetPixel(x,y,Color.FromArgb(random.Next()));
}
//畫圖片的邊框線
graphic.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
//網(wǎng)頁(yè)響應(yīng)
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{
graphic.Dispose();
image.Dispose();
}
}
以上所生成的為簡(jiǎn)單的驗(yàn)證碼。接下來(lái)我從其他的博客中學(xué)習(xí)了其他形式的效果。
/* 圖片畫線特殊效果:貝塞爾曲線 */
Graphics graph = Graphics.FromImage(image);
graph.Clear(Color.WhiteSmoke);
Point[] myArray ={
new Point(random.Next(150),random.Next(50)),
new Point(random.Next(150),random.Next(50)),
new Point(random.Next(150),random.Next(50)),
new Point(random.Next(150),random.Next(50)),
new Point(random.Next(150),random.Next(50)),
new Point(random.Next(150),random.Next(50)),
new Point(random.Next(150),random.Next(50)),
new Point(random.Next(150),random.Next(50)),
new Point(random.Next(150),random.Next(50)),
new Point(random.Next(150),random.Next(50))
};
Pen myPen = new Pen(Color.Blue, 1);
GraphicsPath myPath = new GraphicsPath();
myPath.AddBeziers(myArray);
graph.DrawPath(myPen, myPath);
驗(yàn)證碼字符顏色變換效果:實(shí)現(xiàn)該效果,我們首先來(lái)定義一個(gè)顏色集合,然后通過(guò)for循環(huán)使其隨機(jī)改變字體顏色則可。
#region 定義顏色數(shù)組
Color[] colors = { Color.Blue, Color.Green, Color.Red, Color.Gold, Color.Black, Color.Chocolate, Color.Orange, Color.Purple };
public Color[] Colors
{
get { return colors; }
set { colors = value; }
}
#endregion
Brush brush;
int colornum;
for(int i=0; i
{
colornum = random.Next(Colors.Length - 1);
brush = new System.Drawing.SolidBrush(Colors[cindex]);
//利用DrawString函數(shù)進(jìn)行顏色填充就可以了。
}
同樣的原理我們也可以定義一個(gè)字體的數(shù)組來(lái)進(jìn)行驗(yàn)證碼字體切換。代碼和顏色的類似,這里就不加以累贅。
接下來(lái)看看如何使得驗(yàn)證碼的字體進(jìn)行扭曲。
private const double PI = 3.1415926535897932384626433832795;
private const double PI2 = 6.283185307179586476925286766559;
///
/// 波形濾鏡效果函數(shù)
///
///
///
///
///
///
public System.Drawing.Bitmap TwistImage(Bitmap srcbmp, double dmultvalue, double dphase)
{
System.Drawing.Bitmap destbmp = new Bitmap(srcbmp.Width,srcbmp.Height);
System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destbmp);
//填充背景圖為白色
graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destbmp.Width, destbmp.Height);
graph.Dispose();
double dbaselen = (double)destbmp.Width;
for (int i = 0; i < destbmp.Width; i++)
{
for (int j = 0; j < destbmp.Height; j++)
{
double dx = 0;
dx = (PI2 * (double)j) / dbaselen;
dx += dphase;
double dy = Math.Sin(dx);
int noldx = 0, noldy = 0;
noldx = i + (int)(dy * dmultvalue);
noldy = j + (int)(dy * dmultvalue);
System.Drawing.Color color = srcbmp.GetPixel(i, j);
if (noldx >= 0 && noldx < destbmp.Width && noldy >= 0 && noldy < destbmp.Height)
destbmp.SetPixel(noldx, noldy, color);
}
}
return destbmp;
}
上面代碼是參考了這段代碼進(jìn)行的學(xué)習(xí)<C#.net 好用的驗(yàn)證碼代碼 漢字-變色-扭曲-波動(dòng) >,代碼如下
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class study_CheckCode2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string code = CreateVerifyCode(); //取隨機(jī)碼
CreateImageOnPage(code, this.Context); // 輸出圖片
Response.Cookies.Add(new HttpCookie("CheckCode", code.ToUpper()));// 使用Cookies取驗(yàn)證碼的值
}
#region 驗(yàn)證碼長(zhǎng)度(默認(rèn)4個(gè)驗(yàn)證碼的長(zhǎng)度)
int length = 4;
public int Length
{
get { return length; }
set { length = value; }
}
#endregion
#region 驗(yàn)證碼字體大小(為了顯示扭曲效果,默認(rèn)40像素,可以自行修改)
int fontSize = 40;
public int FontSize
{
get { return fontSize; }
set { fontSize = value; }
}
#endregion
#region 邊框補(bǔ)(默認(rèn)1像素)
int padding = 2;
public int Padding
{
get { return padding; }
set { padding = value; }
}
#endregion
#region 是否輸出燥點(diǎn)(默認(rèn)不輸出)
bool chaos = true;
public bool Chaos
{
get { return chaos; }
set { chaos = value; }
}
#endregion
#region 輸出燥點(diǎn)的顏色(默認(rèn)灰色)
Color chaosColor = Color.LightGray;
public Color ChaosColor
{
get { return chaosColor; }
set { chaosColor = value; }
}
#endregion
#region 自定義背景色(默認(rèn)白色)
Color backgroundColor = Color.White;
public Color BackgroundColor
{
get { return backgroundColor; }
set { backgroundColor = value; }
}
#endregion
#region 自定義隨機(jī)顏色數(shù)組
Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
public Color[] Colors
{
get { return colors; }
set { colors = value; }
}
#endregion
#region 自定義字體數(shù)組
string[] fonts = { "Arial", "Georgia" };
public string[] Fonts
{
get { return fonts; }
set { fonts = value; }
}
#endregion
#region 自定義隨機(jī)碼字符串序列(使用逗號(hào)分隔)
//string codeSerial = "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,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
string codeSerial = "阿,保,持,的,法,規(guī),和,東,三,省,問(wèn),我,惹,你,誒,沒(méi),改,變,揍,屁,股,吧";
public string CodeSerial
{
get { return codeSerial; }
set { codeSerial = value; }
}
#endregion
#region 產(chǎn)生波形濾鏡效果
private const double PI = 3.1415926535897932384626433832795;
private const double PI2 = 6.283185307179586476925286766559;
/// <summary>
/// 正弦曲線Wave扭曲圖片(Edit By 51aspx.com)
/// </summary>
/// <param name="srcBmp">圖片路徑</param>
/// <param name="bXDir">如果扭曲則選擇為True</param>
/// <param name="dMultValue">波形的幅度倍數(shù),越大扭曲的程度越高,一般為3</param>
/// <param name="dPhase">波形的起始相位,取值區(qū)間[0-2*PI)</param>
/// <returns></returns>
public System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
{
System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
// 將位圖背景填充為白色
System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);
graph.FillRectangle(new SolidBrush(System.Drawing.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 ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
dx += dPhase;
double dy = Math.Sin(dx);
// 取得當(dāng)前點(diǎn)的顏色
int nOldX = 0, nOldY = 0;
nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
nOldY = bXDir ? j : j + (int)(dy * dMultValue);
System.Drawing.Color color = srcBmp.GetPixel(i, j);
if (nOldX >= 0 && nOldX < destBmp.Width
&& nOldY >= 0 && nOldY < destBmp.Height)
{
destBmp.SetPixel(nOldX, nOldY, color);
}
}
}
return destBmp;
}
#endregion
#region 生成校驗(yàn)碼圖片
public Bitmap CreateImageCode(string code)
{
int fSize = FontSize;
int fWidth = fSize + Padding;
int imageWidth = (int)(code.Length * fWidth) + 30 + Padding * 2;
int imageHeight = fSize * 2 + Padding;
System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageWidth, imageHeight);
Graphics g = Graphics.FromImage(image);
g.Clear(BackgroundColor);
Random rand = new Random();
//給背景添加隨機(jī)生成的燥點(diǎn)
if (this.Chaos)
{
Pen pen = new Pen(ChaosColor, 0);
int c = Length * 10;
for (int i = 0; i < c; i++)
{
int x = rand.Next(image.Width);
int y = rand.Next(image.Height);
g.DrawRectangle(pen, x, y, 1, 1);
}
}
int left = 0, top = 0, top1 = 1, top2 = 1;
int n1 = (imageHeight - FontSize - Padding * 2);
int n2 = n1 / 4;
top1 = n2;
top2 = n2 * 2;
Font f;
Brush b;
int cindex, findex;
//隨機(jī)字體和顏色的驗(yàn)證碼字符
for (int i = 0; i < code.Length; i++)
{
cindex = rand.Next(Colors.Length - 1);
findex = rand.Next(Fonts.Length - 1);
f = new System.Drawing.Font(Fonts[findex], fSize, System.Drawing.FontStyle.Bold);
b = new System.Drawing.SolidBrush(Colors[cindex]);
if (i % 2 == 1)
{
top = top2;
}
else
{
top = top1;
}
left = i * fWidth;
g.DrawString(code.Substring(i, 1), f, b, left, top);
}
//畫一個(gè)邊框 邊框顏色為Color.Gainsboro
g.DrawRectangle(new Pen(Color.Gainsboro, 0), 0, 0, image.Width - 1, image.Height - 1);
g.Dispose();
//產(chǎn)生波形(Add By 51aspx.com)
image = TwistImage(image, true, 8, 4);
return image;
}
#endregion
#region 將創(chuàng)建好的圖片輸出到頁(yè)面
public void CreateImageOnPage(string code, HttpContext context)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
Bitmap image = this.CreateImageCode(code);
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
context.Response.ClearContent();
context.Response.ContentType = "image/Jpeg";
context.Response.BinaryWrite(ms.GetBuffer());
ms.Close();
ms = null;
image.Dispose();
image = null;
}
#endregion
#region 生成隨機(jī)字符碼
public string CreateVerifyCode(int codeLen)
{
if (codeLen == 0)
{
codeLen = Length;
}
string[] arr = CodeSerial.Split(',');
string code = "";
int randValue = -1;
Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < codeLen; i++)
{
randValue = rand.Next(0, arr.Length - 1);
code += arr[randValue];
}
return code;
}
public string CreateVerifyCode()
{
return CreateVerifyCode(0);
}
#endregion
}
一年一度的搶票熱潮又開(kāi)始了,希望大家都能順利買到回家的火車篇,回家過(guò)年,突然感覺(jué)有點(diǎn)年味了,一年又一年,時(shí)間都去哪了,小小的感慨一下……
言歸正傳,這就是為大家分享的C#驗(yàn)證碼程序,和12306驗(yàn)證碼差多了,不過(guò)也是小編的學(xué)習(xí)收獲吧!大家也可以結(jié)合下面這兩篇文章進(jìn)行學(xué)習(xí):
《12306動(dòng)態(tài)驗(yàn)證碼啟發(fā)之ASP.NET實(shí)現(xiàn)動(dòng)態(tài)GIF驗(yàn)證碼(附源碼)》
《12306驗(yàn)證碼破解思路分享》
希望本文所述對(duì)大家學(xué)習(xí)驗(yàn)證碼技術(shù)有所幫助。
欄 目:C#教程
下一篇:C#實(shí)現(xiàn)啟動(dòng),關(guān)閉與查找進(jìn)程的方法
本文標(biāo)題:12306奇葩驗(yàn)證碼引發(fā)思考之C#實(shí)現(xiàn)驗(yàn)證碼程序
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6797.html
您可能感興趣的文章
- 01-10關(guān)于nancy中的身份驗(yàn)證
- 01-10基于C#實(shí)現(xiàn)簡(jiǎn)單離線注冊(cè)碼生成與驗(yàn)證
- 01-10C#身份證號(hào)碼驗(yàn)證是否正確
- 01-10c#實(shí)現(xiàn)識(shí)別圖片上的驗(yàn)證碼數(shù)字
- 01-10基于C#實(shí)現(xiàn)12306的動(dòng)態(tài)驗(yàn)證碼變成靜態(tài)驗(yàn)證碼的方法
- 01-10理解C#生成驗(yàn)證碼的過(guò)程
- 01-10mvc C# JavaScript LigerUI oracle實(shí)現(xiàn)用戶的注冊(cè)、登陸驗(yàn)證、登陸
- 01-10SMTP客戶端未通過(guò)身份驗(yàn)證等多種錯(cuò)誤解決方案分享
- 01-10WinForm生成驗(yàn)證碼圖片的方法
- 01-10C#實(shí)現(xiàn)的一款比較美觀的驗(yàn)證碼完整實(shí)例


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)


