ASP.NET 實現(xiàn)驗證碼以及刷新驗證碼的小例子
實現(xiàn)代碼
/// <summary>
/// 生成驗證碼圖片,保存session名稱VerificationCode
/// </summary>
public static void CreateVerificationCode()
{
int number;
string checkCode = string.Empty;
//隨機數(shù)種子
Random randoms = new Random();
for (int i = 0; i < 4; i++) //校驗碼長度為4
{
//隨機的整數(shù)
number = randoms.Next();
//字符從0-9,A-Z中隨機產(chǎn)生,對應(yīng)的ASCII碼分別為
//48-57,65-90
number = number % 36;
if (number < 10)
{
number += 48;
}
else
{
number += 55;
}
checkCode += ((char)number).ToString();
}
//在session中保存校驗碼
System.Web.HttpContext.Current.Session["VerificationCode"] = checkCode;
//若校驗碼為空,則直接返回
if (checkCode == null || checkCode.Trim() == String.Empty)
{
return;
}
//根據(jù)校驗碼的長度確定輸出圖片的長度
System.Drawing.Bitmap image = new System.Drawing.Bitmap(55, 20);//(int)Math.Ceiling(Convert.ToDouble(checkCode.Length * 15))
//創(chuàng)建Graphics對象
Graphics g = Graphics.FromImage(image);
try
{
//生成隨機數(shù)種子
Random random = new Random();
//清空圖片背景色
g.Clear(Color.White);
//畫圖片的背景噪音線 10條
//---------------------------------------------------
for (int i = 0; i < 10; i++)
{
//噪音線起點坐標(x1,y1),終點坐標(x2,y2)
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
//用銀色畫出噪音線
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
//---------------------------------------------------
//Brush b = Brushes.Silver;
//g.FillRectangle(b, 0, 0, image.Width, image.Height);
//---------------------以上兩種任選其一------------------------------
//輸出圖片中校驗碼的字體: 12號Arial,粗斜體
Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
//線性漸變畫刷
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Purple, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
//畫圖片的前景噪音點 50個
for (int i = 0; i < 50; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//畫圖片的邊框線
g.DrawRectangle(new Pen(Color.Peru), 0, 0, image.Width - 1, image.Height - 1);
//創(chuàng)建內(nèi)存流用于輸出圖片
using (MemoryStream ms = new MemoryStream())
{
//圖片格式指定為png
image.Save(ms, ImageFormat.Jpeg);
//清除緩沖區(qū)流中的所有輸出
System.Web.HttpContext.Current.Response.ClearContent();
//輸出流的HTTP MIME類型設(shè)置為"image/Png"
System.Web.HttpContext.Current.Response.ContentType = "image/Jpeg";
//輸出圖片的二進制流
System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
}
finally
{
//釋放Bitmap對象和Graphics對象
g.Dispose();
image.Dispose();
}
}
創(chuàng)建一個aspx頁面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AuthCode.aspx.cs" Inherits="AuthCode" %>
<%Help.CreateVerificationCode(); %>
添加HTML代碼,引用
<div class="positionR">
<label>驗證碼:</label>
<span class="style1"> *</span>
<input type="text" class="yanZm" runat="Server" reg="^.+$" id="txtAuthCode" tip="請輸入驗證碼!" />
<img class="yanZm_img" src="AuthCode.aspx" alt="" id="imgAuthCode" />
</div>
如何實現(xiàn)刷新?
<script type="text/javascript">
$("#imgAuthCode").click(function () {
$(this).attr("src", "AuthCode.aspx?code=" + (new Date()).getTime());
});
</script>
效果圖
相關(guān)文章
ASP與ASP.NET互通COOKIES的一點經(jīng)驗
ASP與ASP.NET互通COOKIES的一點經(jīng)驗...2006-09-09ASP.Net PlaceHolder、Panel等控件未實現(xiàn)INamingContainer,導(dǎo)致FindContro
這2天在開發(fā)中發(fā)現(xiàn),如果在new的Panel中使用FindControl,會出現(xiàn)找不到控件的情況2009-06-06ASP.NET在MVC中MaxLength特性設(shè)置無效的解決方法
這篇文章主要介紹了ASP.NET在MVC中MaxLength特性設(shè)置無效的解決方法,涉及對MVC中表單元素屬性的應(yīng)用技巧,需要的朋友可以參考下2014-11-11總結(jié)ASP.NET C#中經(jīng)常用到的13個JS腳本代碼
本文總結(jié)了ASP.NET C#在實際開發(fā)過程中13個JS腳本代碼,方便大家在開發(fā)中使用,希望對大家有用。2016-04-04asp.net(c#)開發(fā)中的文件上傳組件uploadify的使用方法(帶進度條)
在asp.net開發(fā)中,有很多可以上傳的組件模塊,利用HTML的File控件(uploadify)的上傳也是一種辦法,這里為大家介紹一下(uploadify)的一些使用方法2012-12-12