国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net ajax實現無刷新驗證碼

2024-07-10 13:24:36
字體:
來源:轉載
供稿:網友
1、首先是在后臺驗證碼的aspx文件的Page_Load中的事件代碼:

復制代碼 代碼如下:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace 學生在線考試系統
{
public partial class AjaxAutoCode : System.Web.UI.Page
{
//驗證數字
public string authcode = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
#region 第一種產生驗證碼的方法
Random random = new Random();
authcode = random.Next(1111, 9999).ToString();
//構造圖片
Bitmap image = new Bitmap(authcode.Length * 12, 25);
//創建畫布
Graphics g = Graphics.FromImage(image);
try
{
g.Clear(Color.White);
for (int i = 0; i < 25; i++)
{
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);
}
Font font = new Font("Arial", 12, FontStyle.Bold | FontStyle.Italic);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkBlue, 1.2f, true);
g.DrawString(authcode, font, brush, 2, 2);
//畫圖片的前景噪點
for (int i = 0; i < 100; 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.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
ms.WriteTo(this.Response.OutputStream);
ms.Close();
this.Response.ContentType = "image/gif";
}
finally
{
image.Dispose();
g.Dispose();
}
#endregion
}
}
}


2、其次在顯示驗證碼的頁面定義一個JS函數

復制代碼 代碼如下:


function fGetCode()
{
document.getElementById("getcode").src="Default2.aspx?"+Math.random();
}


3.再編輯前臺頁面aspx,下面是前臺頁面的代碼片段

復制代碼 代碼如下:


<label>驗證碼</label>
<asp:TextBox runat="server"></asp:TextBox>
<img src="Default2.aspx" alt="看不清楚?"/> <a href="javascript:fGetCode()">更換驗證碼</a>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 酉阳| 南雄市| 钟祥市| 嘉定区| 东宁县| 芦山县| 平定县| 深泽县| 南陵县| 卢湾区| 萝北县| 娄底市| 西昌市| 泰安市| 巴彦淖尔市| 松溪县| 广宗县| 琼中| 绥滨县| 湖北省| 丰镇市| 宜宾县| 繁峙县| 藁城市| 绥芬河市| 沂水县| 台州市| 濮阳县| 渝北区| 丹巴县| 廉江市| 金湖县| 沈阳市| 泾阳县| 河池市| 镇巴县| 左权县| 容城县| 宝鸡市| 和田县| 璧山县|