ASP.NET 實(shí)現(xiàn)驗(yàn)證碼以及刷新驗(yàn)證碼的小例子
實(shí)現(xiàn)代碼
/// <summary>
/// 生成驗(yàn)證碼圖片,保存session名稱VerificationCode
/// </summary>
public static void CreateVerificationCode()
{
int number;
string checkCode = string.Empty;
//隨機(jī)數(shù)種子
Random randoms = new Random();
for (int i = 0; i < 4; i++) //校驗(yàn)碼長(zhǎng)度為4
{
//隨機(jī)的整數(shù)
number = randoms.Next();
//字符從0-9,A-Z中隨機(jī)產(chǎn)生,對(duì)應(yīng)的ASCII碼分別為
//48-57,65-90
number = number % 36;
if (number < 10)
{
number += 48;
}
else
{
number += 55;
}
checkCode += ((char)number).ToString();
}
//在session中保存校驗(yàn)碼
System.Web.HttpContext.Current.Session["VerificationCode"] = checkCode;
//若校驗(yàn)碼為空,則直接返回
if (checkCode == null || checkCode.Trim() == String.Empty)
{
return;
}
//根據(jù)校驗(yàn)碼的長(zhǎng)度確定輸出圖片的長(zhǎng)度
System.Drawing.Bitmap image = new System.Drawing.Bitmap(55, 20);//(int)Math.Ceiling(Convert.ToDouble(checkCode.Length * 15))
//創(chuàng)建Graphics對(duì)象
Graphics g = Graphics.FromImage(image);
try
{
//生成隨機(jī)數(shù)種子
Random random = new Random();
//清空?qǐng)D片背景色
g.Clear(Color.White);
//畫(huà)圖片的背景噪音線 10條
//---------------------------------------------------
for (int i = 0; i < 10; i++)
{
//噪音線起點(diǎn)坐標(biāo)(x1,y1),終點(diǎn)坐標(biāo)(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);
//用銀色畫(huà)出噪音線
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
//---------------------------------------------------
//Brush b = Brushes.Silver;
//g.FillRectangle(b, 0, 0, image.Width, image.Height);
//---------------------以上兩種任選其一------------------------------
//輸出圖片中校驗(yàn)碼的字體: 12號(hào)Arial,粗斜體
Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
//線性漸變畫(huà)刷
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);
//畫(huà)圖片的前景噪音點(diǎn) 50個(gè)
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()));
}
//畫(huà)圖片的邊框線
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";
//輸出圖片的二進(jìn)制流
System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
}
finally
{
//釋放Bitmap對(duì)象和Graphics對(duì)象
g.Dispose();
image.Dispose();
}
}
創(chuàng)建一個(gè)aspx頁(yè)面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AuthCode.aspx.cs" Inherits="AuthCode" %>
<%Help.CreateVerificationCode(); %>
添加HTML代碼,引用
<div class="positionR">
<label>驗(yàn)證碼:</label>
<span class="style1"> *</span>
<input type="text" class="yanZm" runat="Server" reg="^.+$" id="txtAuthCode" tip="請(qǐng)輸入驗(yàn)證碼!" />
<img class="yanZm_img" src="AuthCode.aspx" alt="" id="imgAuthCode" />
</div>
如何實(shí)現(xiàn)刷新?
<script type="text/javascript">
$("#imgAuthCode").click(function () {
$(this).attr("src", "AuthCode.aspx?code=" + (new Date()).getTime());
});
</script>
效果圖

- Asp.net Web Api實(shí)現(xiàn)圖片點(diǎn)擊式圖片驗(yàn)證碼功能
- asp.net點(diǎn)選驗(yàn)證碼實(shí)現(xiàn)思路分享 (附demo)
- ASP.NET中畫(huà)圖形驗(yàn)證碼的實(shí)現(xiàn)代碼
- Asp.net開(kāi)發(fā)之webform圖片水印和圖片驗(yàn)證碼的實(shí)現(xiàn)方法
- asp.net登錄驗(yàn)證碼實(shí)現(xiàn)方法
- asp.net之生成驗(yàn)證碼的方法集錦(一)
- 如何使用ASP.NET制作簡(jiǎn)單的驗(yàn)證碼
- asp.net驗(yàn)證碼圖片生成示例
- Asp.net實(shí)現(xiàn)手寫(xiě)驗(yàn)證碼的操作代碼
相關(guān)文章
.net c# gif動(dòng)畫(huà)如何添加圖片水印實(shí)現(xiàn)思路及代碼
本文將詳細(xì)介紹下c#實(shí)現(xiàn)gif動(dòng)畫(huà)添加圖片水印,思路很清晰,感興趣的你可以參考下哈,希望可以幫助到你2013-03-03
ASP.NET Core WebApi中使用FluentValidation驗(yàn)證數(shù)據(jù)模型的方法
這篇文章主要介紹了ASP.NET Core WebApi中使用FluentValidation驗(yàn)證數(shù)據(jù)模型的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
詳解如何在ASP.Net Core中實(shí)現(xiàn)健康檢查
這篇文章主要介紹了詳解如何在ASP.Net Core中實(shí)現(xiàn)健康檢查,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
VS2005打開(kāi)VS2008項(xiàng)目的2種方法(vs2005怎么打開(kāi)2008)
vs2008支持.net3.5,而vs2005支持.net2.0,所以使用vs2005打開(kāi)vs2008的項(xiàng)目,要確定你的項(xiàng)目是.net2.0的,下面介紹二種VS2005打開(kāi)VS2008項(xiàng)目的方法2014-01-01
asp.net 讀取文本文件并插入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼
最近我司和招行有合作,招行給財(cái)務(wù)的是一個(gè)txt格式的賬務(wù)文本文件,文本文件包含很多內(nèi)容,對(duì)賬只需要用到其中一部分內(nèi)容。2010-04-04
.NET實(shí)現(xiàn)WebSocket服務(wù)端即時(shí)通信實(shí)例
本篇文章主要介紹了.NET實(shí)現(xiàn)即時(shí)通信,WebSocket服務(wù)端實(shí)例 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02

