完整asp.net圖形驗證碼程序

1、測試頁面:Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"? CodeFile="Default.aspx.cs" Inherits="_Default" %>
?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
?
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
??? <title>ASP.NET驗證碼</title>
</head>
<body>
??? <form id="form1" runat="server">
??? <div>
??????? 請輸入驗證碼:<input id="txtVI" type="text" name="view_id" runat="server" maxlength="5" size="5"/>
???? <asp:Image id="Image1" runat="server" ImageUrl="ValidateCode.aspx" ImageAlign="Middle" />
??????? <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="您沒有輸入驗證碼" ControlToValidate="txtVI"></asp:RequiredFieldValidator><br />
??????? <asp:Button ID="Button1" runat="server" Text="提 交" OnClick="Button1_Click" /></div>
??? </form>
</body>
</html>

2、測試頁面后臺:Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
??? protected void Page_Load(object sender, EventArgs e)
??? {
?
??? }
??? protected void Button1_Click(object sender, EventArgs e)
??? {
??????? string VNum;
??????? VNum = Session["VNum"].ToString();
??????? Session.Abandon();
??????? ViewState["VNum"] = VNum;
??????? if (txtVI.Value == ViewState["VNum"].ToString())
??????? {
??????????? Response.Write("<script>alert('驗證碼輸入正確!');</script>");
??????????? return;
??????? }
??????? else
??????? {
??????????? // VIWShow.Text="所填寫的驗證碼不正確!";
??????????? Response.Write("<script>alert('驗證碼輸入錯誤!');</script>");
??????????? return;
??????? }
??? }
}

3、生成驗證碼頁面:ValidateCode.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ValidateCode.aspx.cs" Inherits="ValidateCode" %>

4、生成驗證碼頁面后臺:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Drawing;
?
/* Copyright all(c) 2005 ZhongFeng, http://blog.csdn.net/SW515 */
public partial class ValidateCode : System.Web.UI.Page
{
??? private void Page_Load(object sender, System.EventArgs e)
??? {
??????? this.CreateCheckCodeImage(GenerateCheckCode());
??? }
?
??? #region Web 窗體設計器生成的代碼
??? override protected void OnInit(EventArgs e)
??? {
??????? //
??????? // CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
??????? //
??????? InitializeComponent();
??????? base.OnInit(e);
??? }
?
??? /// <summary>
??? /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
??? /// 此方法的內容。
??? /// </summary>
??? private void InitializeComponent()
??? {
??????? this.Load += new System.EventHandler(this.Page_Load);
??? }
??? #endregion
?
??? private string GenerateCheckCode()
??? {
??????? int number;
??????? char code;
??????? string checkCode = String.Empty;
?
??????? System.Random random = new Random();
?
??????? for (int i = 0; i < 5; i++)
??????? {
??????????? number = random.Next();
?
??????????? if (number % 2 == 0)
??????????????? code = (char)('0' + (char)(number % 10));
??????????? else
??????????????? code = (char)('A' + (char)(number % 26));
?
??????????? checkCode += code.ToString();
??????? }
?
??????? Session["VNum"] = checkCode;
?
??????? return checkCode;
??? }
?
??? private void CreateCheckCodeImage(string checkCode)
??? {
??????? if (checkCode == null || checkCode.Trim() == String.Empty)
??????????? return;
?
??????? System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
??????? System.Drawing.Graphics g = Graphics.FromImage(image);
?
??????? try
??????? {
??????????? //生成隨機生成器
??????????? Random random = new Random();
?
??????????? //清空圖片背景色
??????????? 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 System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
??????????? System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
??????????? g.DrawString(checkCode, 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);
??????????? Response.ClearContent();
??????????? Response.ContentType = "image/Gif";
??????????? Response.BinaryWrite(ms.ToArray());
??????? }
??????? finally
??????? {
??????????? g.Dispose();
??????????? image.Dispose();
??????? }
??? }
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/276457.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/276457.shtml
英文地址,請注明出處:http://en.pswp.cn/news/276457.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

學習 redux 源碼整體架構,深入理解 redux 及其中間件原理

如果覺得內容不錯&#xff0c;可以設為星標置頂我的公眾號1. 前言你好&#xff0c;我是若川。這是學習源碼整體架構系列第八篇。整體架構這詞語好像有點大&#xff0c;姑且就算是源碼整體結構吧&#xff0c;主要就是學習是代碼整體結構&#xff0c;不深究其他不是主線的具體函數…

pdf安裝包_有么有pdf控件,不需要用戶安裝任何安裝包直接打印的?

如果開發一個軟件&#xff0c;需要用到PDF功能&#xff0c;您的選擇是基于Adobe PDF嗎&#xff1f; 如果是基于Adobe PDF&#xff0c;需要用戶安裝一個幾十M的Adobe的安裝包&#xff0c;這顯然是不友好的。即使目前也有了一些其它的閱讀器&#xff0c;大小也還好。但是&#xf…

Centos編譯安裝Apache 2.4.6筆記 配置

轉載鏈接&#xff1a;http://www.onepx.com/centos-apache-246.html 之前服務器 Apache 版本一直是 2.2.x&#xff0c;鑒于 Centos 更新軟件的惰性&#xff0c;我看直到 2014 年結束&#xff0c;apache 2.4 都不一定會出現在 Centos 中&#xff0c;我是不打算等了&#xff0c;…

[轉] C#異步操作

Title 通過委托實現異步調用中BeginInvoke及回調函數的使用 通過委托實現異步調用的步驟&#xff1a; 1.定義委托。 2.將要進行異步調用的方法“實例化”到定義的委托。 3.在委托上調用BeginInvoke方法。其中&#xff0c;BeginInvoke的參數由三個部分構成。第一部分&#xff1…

HTTP Server Error 500 內部服務器錯誤

問題&#xff1a;HTTP500錯誤 或 Server Application Error ------------------------------------Server Application ErrorThe server has encountered an error while loading an application during the processing of your request. Please refer to the event log for mo…

使用 ohmyzsh 打造 windows、ubuntu、mac 系統高效終端命令行工具

如果覺得內容不錯&#xff0c;可以設為星標置頂我的公眾號原標題名&#xff1a;oh my zsh 和 windows git bash 設置別名提高效率寫于2018年06月03日在我的微信交流群中聽聞很多前端開發比較貧窮&#xff0c;沒有買mac電腦&#xff08;比如我&#xff09;&#xff0c;也沒有用過…

request獲取mac地址_【Go】獲取用戶真實的ip地址

原文鏈接&#xff1a;https://blog.thinkeridea.com/201903/go/get_client_ip.html用戶請求到達提供服務的服務器中間有很多的環節&#xff0c;導致服務獲取用戶真實的 ip 非常困難&#xff0c;大多數的框架及工具庫都會封裝各種獲取用戶真實 ip 的方法&#xff0c;在 exnet 包…

Installation of Apache HTTPD

轉載鏈接&#xff1a;http://www.linuxfromscratch.org/blfs/view/svn/server/apache.html Installation of Apache-2.4.7 HTTPD For security reasons, running the server as an unprivileged user and group is strongly encouraged. Create the following group and user…

iPhone開發四劍客之《Objective-C基礎教程》

iPhone 開發四劍客之《Objective-C 基礎教程》 Objective-C 語言是 C 語言的一個擴展集&#xff0c;許多&#xff08;可能是大多數&#xff09;具備 Mac OS X 外觀的應用程序都是使用該語言開發的。它以 C 語言為基礎&#xff0c;添加了一些微妙但意義重大的特性。 蘋果公司為…

教師節,你記憶中老師說過印象最深的是什么話?(抽獎)

我記憶中老師說過印象最深的話小學老師&#xff1a;1、小學語文老師李老師說&#xff0c;以后你們可能帶個手機就可以支付了~不需要帶現金。&#xff08;在杭州確實實現了&#xff0c;用支付寶即可&#xff09; 2、小學數學老師李老師說&#xff1a;好好讀書的目的是啥&#xf…

Spark List組件滾動條加事件使datalist數據發生變化

<?xml version"1.0" encoding"utf-8"?><!-- http://blog.flexexamples.com/2009/05/31/detecting-when-the-vertical-scroll-bar-is-scrolled-on-a-spark-list-control-in-flex-4/ --><s:Application name"Spark_List_scroller_vert…

keras訓練完以后怎么預測_還在使用“龜速”的單顯卡訓練模型?動動手,讓TPU節省你的時間...

點擊上方關注&#xff0c;All in AI中國本文將介紹如何使用Keras和Google CoLaboratory與TPU一起訓練LSTM模型&#xff0c;與本地計算機上的GPU相比&#xff0c;這樣訓練能大大縮短訓練時間。很長一段時間以來&#xff0c;我都在單張GTX 1070顯卡上訓練我的模型&#xff0c;它的…

PHP5加載|安裝外部C動態庫

[1] cd php-5.3.9/ext[2] ./ext_skel --extnamencdocxml[3] cd ncdocxml[4] nano -w config.m4############刪除 3 個 dnldnl PHP_ARG_WITH(my_module, for my_module support,dnl Make sure that the comment is aligned:dnl [ --with-my_module Include my_module support])或…

手把手教你寫個小程序定時器管理庫

背景凹凸曼是個小程序開發者&#xff0c;他要在小程序實現秒殺倒計時。于是他不假思索&#xff0c;寫了以下代碼&#xff1a;Page({init: function () {clearInterval(this.timer)this.timer setInterval(() > {// 倒計時計算邏輯console.log(setInterval)})}, })可是&…

[New Portal]Windows Azure Virtual Machine (14) 在本地制作數據文件VHD并上傳至Azure(1)

《Windows Azure Platform 系列文章目錄》 之前的內容里&#xff0c;我介紹了如何將本地的Server 2012中文版 VHD上傳至Windows Azure&#xff0c;并創建基于該Server 2012 VHD的虛擬機。 我們知道&#xff0c;VHD不僅僅可以保存操作系統&#xff0c;而且可以保存數據文件。 如…

python 退出程序_Python:用Ctrl+C解決終止多線程程序的問題!(建議收藏)

前言&#xff1a;今天為大家帶來的內容是Python:用CtrlC解決終止多線程程序的問題&#xff01;文章中的代碼具有不錯的參考意義&#xff0c;希望在此能夠幫助到各位&#xff01;(多數代碼用圖片的方式呈現出來&#xff0c;方便各位觀看與收藏)出發點&#xff1a;前段時間&#…

Mysql InnoDB Plugin安裝 install

轉載鏈接&#xff1a;http://www.orczhou.com/index.php/2010/03/innodb-plugin-setup/ InnoDB Plugin較之Built-in版本新增了很多特性&#xff1a;包括快速DDL、壓縮存儲等&#xff0c;而且引入了全新的文件格式Barracuda。眾多測試也表明&#xff0c;Plugin在很多方面優于Bu…

Hibernate的數據過濾查詢

數據過濾并不是一種常規的數據查詢方法&#xff0c;而是一種整體的篩選方法。數據過濾也可對數據進行篩選&#xff0c;因此&#xff0c;將其放在Hibernate的數據查詢框架中介紹。 如果一旦啟用了數據過濾器&#xff0c;則不管數據查詢&#xff0c;還是數據加載&#xff0c;該過…

若川知乎高贊:有哪些必看的 JS 庫?

歡迎星標我的公眾號&#xff0c;回復加群&#xff0c;長期交流學習我的知乎回答目前2w閱讀量&#xff0c;270贊&#xff0c;現在發到公眾號聲明原創。必看的js庫&#xff1f;只有當前階段值不值看。我從去年7月起看一些前端庫的源碼&#xff0c;歷時一年才寫了八篇《學習源碼整…

python用for循環求10的因數_python for循環練習(初級)

for循環練習1for i in range(4):print(i)D:\尚硅谷Python\venv\Scripts\python.exe D:/尚硅谷Python/test.py0123for循環練習2for x in range(1,40,5): #間隔5print(x)D:\尚硅谷Python\venv\Scripts\python.exe D:/尚硅谷Python/test.py16111621263136打印99乘法表for i in ran…