浅析使用ASP.NET生成随机验证码图片
最近几天尝试着自己一个人开发项目,课外之余接触了很多以前课上没有教的课外知识,“程序员很多知识都是自学的”这句话一点没错,不管身处什么环境 和人群,都是次要的,许多时候我们少些抱怨、多些行动,一定会有很多意外收获,好了,分享一下今天的收获,希望大家不要嫌弃,上代码。
在ASP页面中生成图片验证码,需要用到System.Drawing命名空间下的很多类,首先我们需要新建一个CreateImage.aspx页面,在后台代码中定义用于生成验证码图片的方法,如下:
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Drawing; public partial class CreateImage : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { //产生4位验证码 string CheckCode = CreateCode(4); //用于验证 Session[ "code" ] = CheckCode; CreateImages(CheckCode); } /// <summary> /// 产生验证码 /// </summary> /// <param name="codeLength"></param> /// <returns></returns> public string CreateCode( int codeLength) { string so = "1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z" ; string [] strArray = so.Split( ',' ); string code = "" ; Random rand = new Random(); for ( int i = 0; i < codeLength; i++) { //Random.Next(minValue,maxValue); //一个大于或等于 minValue 且小于 maxValue 的 32 位带符号整数,即:返回的值范围包括 minValue 但不包括 maxValue。如果 //minValue 等于 maxValue,则返回 minValue。 code += strArray[rand.Next(0, strArray.Length)]; } return code; } /// <summary> /// 产生验证图片 /// </summary> /// <param name="code"></param> public void CreateImages( string code) { //创建一个Bitmap新实例 Bitmap image = new Bitmap(60, 20); Graphics g = Graphics.FromImage(image); WebColorConverter ww = new WebColorConverter(); //清楚整个绘图面,并以制定颜色填充 g.Clear((Color)ww.ConvertFromString( "#FAE264" )); Random rand = new Random(); //画图片的背景噪音线 for ( int i = 0; i < 12; i++) { int x1 = rand.Next(image.Width); int x2 = rand.Next(image.Height); int y1 = rand.Next(image.Width); int y2 = rand.Next(image.Height); g.DrawLine( new Pen(Color.LightGray), x1, y1, x2, y2); } //新建字体 Font font = new Font( "Arial" , 15, 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.Gray, 1.2f, true ); g.DrawString(code, font, brush, 0, 0); //画图片的前景噪音 for ( int i = 0; i < 10; i++) { int x = rand.Next(image.Width); int y = rand.Next(image.Height); image.SetPixel(x, y, Color.White); } //画图片的边框线 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()); g.Dispose(); image.Dispose(); } #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 } |
以上是CreateImage.aspx文件的后台代码,完成上述方法后,在页面里调用方法,只需要添加<img src="CreateImage.aspx" align="middle"/>,即可显示生成的验证码图片,每次刷新都会随机产生不同的验证码,如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateImage.aspx.cs" Inherits="CreateImage" %> <! 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 >CheckImage</ title > </ head > < body > < form id="form1" runat="server"> < div > < img src="CreateImage.aspx" align="middle" /> </ div > </ form > </ body > </ html > |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库