1首先建一個(gè)aspx頁面代碼如下
using System;
using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Web.AjaxPage.OtherRequest
{ public partial class VialCode : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //設(shè)置不緩存此頁 Response.AppendHeader("pragma", "no-cache"); Response.AppendHeader("Cache-Control", "no-cache, must-revalidate"); Response.AppendHeader("expires", "0"); Random rand = new Random();
//獲取隨機(jī)字符
string str = GetRandString(4); Session["nowcodecode"] = str;//保存驗(yàn)證碼 //創(chuàng)建畫板
Bitmap image = new Bitmap(63, 25); Graphics g = Graphics.FromImage(image); //g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.InterpolationMode = InterpolationMode.Low; //g.CompositingMode = CompositingMode.SourceOver; //g.CompositingQuality = CompositingQuality.HighQuality; g.CompositingQuality = CompositingQuality.HighSpeed; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; g.SmoothingMode = SmoothingMode.AntiAlias; //繪制漸變背景
Rectangle rect = new Rectangle(0, 0, image.Width, image.Height); Brush brushBack = new LinearGradientBrush(rect, Color.FromArgb(rand.Next(150, 256), 255, 255), Color.FromArgb(255, rand.Next(150, 256), 255), rand.Next(90)); g.FillRectangle(brushBack, rect); //繪制干擾曲線
for (int i = 0; i < 2; i++) { Point p1 = new Point(0, rand.Next(image.Height)); Point p2 = new Point(rand.Next(image.Width), rand.Next(image.Height)); Point p3 = new Point(rand.Next(image.Width), rand.Next(image.Height)); Point p4 = new Point(image.Width, rand.Next(image.Height)); Point[] p = { p1, p2, p3, p4 }; Pen pen = new Pen(Color.Gray, 1); g.DrawBeziers(pen, p); } //逐個(gè)繪制文字
for (int i = 0; i < str.Length; i++) { string strChar = str.Substring(i, 1); int deg = rand.Next(-15, 15); float x = (image.Width / str.Length / 2) + (image.Width / str.Length) * i; float y = image.Height / 2; //隨機(jī)字體大小 Font font = new Font("Consolas", rand.Next(16, 24), System.Drawing.FontStyle.Regular); SizeF size = g.MeasureString(strChar, font); Matrix m = new Matrix(); //旋轉(zhuǎn) m.RotateAt(deg, new PointF(x, y), MatrixOrder.Append); //扭曲 m.Shear(rand.Next(-10, 10) * 0.03f, 0); g.Transform = m; //隨機(jī)漸變畫筆 Brush brushPen = new LinearGradientBrush(rect, Color.FromArgb(rand.Next(0, 256), 0, 0), Color.FromArgb(0, 0, rand.Next(0, 256)), rand.Next(90)); g.DrawString(str.Substring(i, 1), font, brushPen, new PointF(x - size.Width / 2, y - size.Height / 2)); g.Transform = new Matrix();
} g.Save();
Response.ContentType = "image/jpeg"; Response.Clear(); Response.BufferOutput = true; image.Save(Response.OutputStream, ImageFormat.Jpeg); g.Dispose(); image.Dispose(); Response.Flush(); } //獲取隨機(jī)數(shù)
private string GetRandString(int len) { //string s = "0123456789"; string s = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string str = ""; Random r = new Random(); for (int i = 0; i < len; i++) { str += s.Substring(r.Next(s.Length), 1); } return str; } } } 2顯示驗(yàn)證碼圖片的地方
<td id="td1"><span id="vcodetxt"></span>驗(yàn)證碼:</td> <td> <input type="text" class="input" id="txtcode" style="width:55px;" /> </td> <td> <img alt="刷新驗(yàn)證碼" src="AjaxPage/OtherRequest/VialCode.aspx" id="codeimage" onclick="this.src=this.src+'?'" /> </td> 3怎么校驗(yàn)驗(yàn)證碼是否輸入正確
當(dāng)然產(chǎn)生的時(shí)候就存入session然后登陸的時(shí)候和session比較就OK啦
4注意:onclick="this.src=this.src+'?'"這個(gè)是為了點(diǎn)擊圖片的時(shí)候從新生成
5效果如下
![]() |
|