ASP.NET生成驗(yàn)證碼的方法
本文實(shí)例為大家分享了ASP.NET生成驗(yàn)證碼的具體代碼,供大家參考,具體內(nèi)容如下
首先,添加一個(gè)一般處理程序
注釋很詳細(xì)了,有不懂的歡迎評(píng)論
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Web; using System.Web.SessionState; namespace Project_Practice { /// <summary> /// Handler1 的摘要說(shuō)明 /// </summary> public class Handler1 : IHttpHandler,IRequiresSessionState { public void ProcessRequest(HttpContext context) { //選取的顏色 Color[] colors = { Color.White }; //通過(guò)Bitmap構(gòu)造Image Image img = new Bitmap(100, 60); //Graphics繪畫(huà)Image Graphics graphics = Graphics.FromImage(img); Random random = new Random(DateTime.Now.Millisecond); //驗(yàn)證碼的四位數(shù) int charNum1 = random.Next('0', '9' + 1); int charNum2 = random.Next('0', '9' + 1); int charNum3 = random.Next('0', '9' + 1); int charNum4 = random.Next('0', '9' + 1); //把生成的隨機(jī)數(shù)變成字符串,通過(guò)char進(jìn)行轉(zhuǎn)換 string validCode = string.Format($"{(char)charNum1}{(char)charNum2}{(char)charNum3}{(char)charNum4}"); //放進(jìn)Session進(jìn)行存儲(chǔ),記得繼承接口,否則瘋狂報(bào)空指針 context.Session["verification_Code"] = validCode; //字體的大小和類(lèi)別 Font font = new Font("宋體", 24); //隨機(jī)的顏色 Brush brush1 = new SolidBrush(colors[random.Next(0, colors.Length - 1)]); //DrawString的四個(gè)參數(shù),第一個(gè)是要寫(xiě)的字符,第二個(gè)是字體,第三個(gè)是顏色,第四個(gè)是坐標(biāo)x,y graphics.DrawString(((char)charNum1).ToString(), font, brush1, 7, -3); Brush brush2 = new SolidBrush(colors[random.Next(0, colors.Length - 1)]); graphics.DrawString(((char)charNum2).ToString(), font, brush2, 26, -9); Brush brush3 = new SolidBrush(colors[random.Next(0, colors.Length - 1)]); graphics.DrawString(((char)charNum3).ToString(), font, brush3, 50, 0); Brush brush4 = new SolidBrush(colors[random.Next(0, colors.Length - 1)]); graphics.DrawString(((char)charNum4).ToString(), font, brush4, 70, -7); //保存,格式 context.Response.ContentType = "image/jpeg"; img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); //釋放資源 graphics.Dispose(); img.Dispose(); } public bool IsReusable { get { return false; } } } }
一個(gè)web窗體
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="verification_Code.aspx.cs" Inherits="Project_Practice.verification_Code" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Image ID="Image1" runat="server" ImageUrl="~/Handler1.ashx" /> </div> </form> </body> </html>
效果圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET中 Execl導(dǎo)出的六種方法實(shí)例
這篇文章主要介紹了ASP.NET中 Execl導(dǎo)出的六種方法實(shí)例,有需要的朋友可以參考一下2013-12-12比較簡(jiǎn)單的將數(shù)據(jù)信息導(dǎo)入wrod文檔方案(C# for word)
史上最簡(jiǎn)單將數(shù)據(jù)信息導(dǎo)入wrod文檔方案(C# for word)2010-01-01代碼實(shí)現(xiàn)打印功能(asp.net+javascript)
頁(yè)面實(shí)現(xiàn)打印的效果代碼,分為服務(wù)器端和客戶(hù)端單個(gè)即可,客戶(hù)端的比較不錯(cuò),本站也是類(lèi)似的方法。2009-05-05.net Core連接MongoDB數(shù)據(jù)庫(kù)的步驟詳解
這篇文章主要給大家介紹了關(guān)于.net Core連接MongoDB數(shù)據(jù)庫(kù)步驟的相關(guān)資料,文中將實(shí)現(xiàn)的步驟一步步介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02ASP.NET中DES加密與解密MD5加密幫助類(lèi)的實(shí)現(xiàn)代碼
這篇文章主要介紹了ASP.NET中DES加密與解密MD5加密幫助類(lèi)的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-07-07asp.net 仿騰訊微薄提示 還能輸入*個(gè)字符 的實(shí)現(xiàn)代碼
asp.net 仿騰訊微薄提示 還能輸入*個(gè)字符 的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-10-10在Linux上使用OpenCvSharp的過(guò)程詳解
在本次項(xiàng)目中,我們成功實(shí)現(xiàn)了在Linux上使用OpenCvSharp,并成功配置了OpenCvSharp依賴(lài)庫(kù),實(shí)現(xiàn)了在.NET 6.0環(huán)境下使用C#語(yǔ)言調(diào)用OpenCvSharp庫(kù),實(shí)現(xiàn)的圖片數(shù)據(jù)的讀取以及圖像色彩轉(zhuǎn)換,并進(jìn)行了圖像展示,感興趣的朋友跟隨小編一起看看吧2024-02-02